「Sendgrid」の版間の差分
(ページの作成:「 ==apiを作成== Settings/API keys ==新ドメイン登録連携== 2018/4/18以後 #sendgridのsettings/SenderAuthenticationからドメイン登録する #WhichDomainN...」) |
|||
| 行76: | 行76: | ||
$mail->setMailSettings($setting); | $mail->setMailSettings($setting); | ||
// ここまでbcc処理 | // ここまでbcc処理 | ||
| + | $response = $sg->client->mail()->send()->post($mail); | ||
| + | echo $response->statusCode(); | ||
| + | print_r($response->headers()); | ||
| + | echo $response->body(); | ||
| + | |||
| + | |||
| + | ===添付ファイルサンプル=== | ||
| + | $apiKey = getenv('SENDGRID_API_KEY'); | ||
| + | $sg = new \SendGrid($apiKey); | ||
| + | $from = new SendGrid\Email("Example User", "test@example.com"); | ||
| + | $subject = "Sending with SendGrid is Fun"; | ||
| + | $to = new SendGrid\Email("Example User", "test@example.com"); | ||
| + | $content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP"); | ||
| + | $mail = new SendGrid\Mail($from, $subject, $to, $content); | ||
| + | // ここから添付ファイル処理 | ||
| + | $filePath = __DIR__ . "/hogehoge.png"; | ||
| + | $attachment = new SendGrid\Attachment(); | ||
| + | $handle = fopen($filePath, "rb"); | ||
| + | $contents = fread($handle, filesize($filePath)); | ||
| + | $attachment->setContent(base64_encode($contents)); | ||
| + | $attachment->setFilename("filename.png"); | ||
| + | $fileInfo = new FInfo(FILEINFO_MIME_TYPE); | ||
| + | $attachment->setType($fileInfo->file($filePath)); | ||
| + | $mail->addAttachment($attachment); | ||
| + | // ここまで添付ファイル処理 | ||
$response = $sg->client->mail()->send()->post($mail); | $response = $sg->client->mail()->send()->post($mail); | ||
echo $response->statusCode(); | echo $response->statusCode(); | ||
2018年5月11日 (金) 12:33時点における版
目次
apiを作成
Settings/API keys
新ドメイン登録連携
2018/4/18以後
- sendgridのsettings/SenderAuthenticationからドメイン登録する
- WhichDomainNameServerでDNSHostを選択(選択にない場合は入力不要)
- linksは後で設定可能なのでNoとする
- DomainYouSendFromにドメインを追加する(サブドメでも良い)
- AdvancedSettingを開きUse automaticed securityをonにしておく
- 表示された3つのcnameを登録する
参考:https://sendgrid.kke.co.jp/docs/Tutorials/D_Improve_Deliverability/using_whitelabel.html
旧ドメイン登録連携
2018/4/18以前
- sendgridのsettings/whitelabelからドメイン登録する
- AUTOMATED SECURITYをonにしておく
- 表示された3つのcnameを登録する
DKIM,SPF
上記ドメイン連携すれば適応できる
phpの場合
composer設定
composer.json
{
"require": {
"sendgrid/sendgrid": "~6.0"
}
}
サンプル
<?php
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
// If you are using Composer (recommended)
require 'vendor/autoload.php';
// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");
$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "test@example.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
https://sendgrid.kke.co.jp/docs/Integrate/Code_Examples/v3_Mail/php.html
https://github.com/sendgrid/sendgrid-php#usage
https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
bccサンプル
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "test@example.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
// ここからbcc処理
$bcc = new SendGrid\BccSettings();
$bcc->setEmail("bcc@example.com");
$bcc->setEnable(true);
$setting = new SendGrid\MailSettings();
$setting->setBccSettings($bcc);
$mail->setMailSettings($setting);
// ここまでbcc処理
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
添付ファイルサンプル
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "test@example.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
// ここから添付ファイル処理
$filePath = __DIR__ . "/hogehoge.png";
$attachment = new SendGrid\Attachment();
$handle = fopen($filePath, "rb");
$contents = fread($handle, filesize($filePath));
$attachment->setContent(base64_encode($contents));
$attachment->setFilename("filename.png");
$fileInfo = new FInfo(FILEINFO_MIME_TYPE);
$attachment->setType($fileInfo->file($filePath));
$mail->addAttachment($attachment);
// ここまで添付ファイル処理
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
ステータスコード一覧
https://sendgrid.kke.co.jp/docs/API_Reference/Web_API_v3/How_To_Use_The_Web_API_v3/responses.html
サブアカウントを作成
Settings/Teammatesで作成する
サブユーザを作成
複数の独立した環境(子アカウント)を作成する機能。 テスト環境や、送信するメールの種類に応じて環境を使い分けることができる。
Settings/SubuserManagementでアカウントを作成すれば、そのアカウントで同様にsendgrid管理システムに入れる
改行について
\r\nを2つ並べると改行される
$content = new SendGrid\Content("text/plain", "hoge\r\n\r\n");
oemについて
SendGridを自社サービスに組み込む際の注意事項 https://support.sendgrid.kke.co.jp/hc/ja/articles/205590193 にあるとおり、 https://sendgrid.kke.co.jp/terms/ のOEM利用規約を盛り込む必要があり、sendgridの問い合わせフォームに連絡する必要がある。
宛先指定可能件数
1つのコンテンツに対して1000件まで https://sendgrid.kke.co.jp/docs/API_Reference/Web_API_v3/Mail/index.html
秒間リクエスト件数
10000リクエスト/秒 https://sendgrid.kke.co.jp/docs/API_Reference/Web_API_v3/Mail/index.html
