facebook twitter hatena line email

Sendgrid

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

apiを作成

Settings/API keys

新ドメイン登録連携

2018/4/18以後

  1. sendgridのsettings/SenderAuthenticationからドメイン登録する
  2. WhichDomainNameServerでDNSHostを選択(選択にない場合は入力不要)
  3. linksは後で設定可能なのでNoとする
  4. DomainYouSendFromにドメインを追加する(サブドメでも良い)
  5. AdvancedSettingを開きUse automaticed securityをonにしておく
  6. 表示された3つのcnameを登録する

参考:https://sendgrid.kke.co.jp/docs/Tutorials/D_Improve_Deliverability/using_whitelabel.html

旧ドメイン登録連携

2018/4/18以前

  1. sendgridのsettings/whitelabelからドメイン登録する
  2. AUTOMATED SECURITYをonにしておく
  3. 表示された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();


マルチパート(text and html)サンプル

$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);
// ここから添付ファイル処理
$mail->addContent(new SendGrid\Content("text/html", "and <b>hoge</b><br /><font color=red>red</font>"));
// ここまで添付ファイル処理
$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管理システムに入れる

改行されない場合

テキストのみの場合は自動でhtmlを生成するようになっている。 settings/MailSettings/PlainContentをoffにすると自動html生成機能を切ることができるが、 urlなどがあると自動で機能するようで。pタグで囲ってしまう。 どうしてもだめなら、htmlを手動で追加してマルチパートで送れば良い。

$content = new SendGrid\Content("text/plain", "hoge\nhoge\n");
$mail->addContent(new SendGrid\Content("text/html", "hoge<br />hoge<br />"));

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

sendgridのメール送信状態

管理画面/Activityで確認できる。(7日で消える)

Processed:メール送信リクエストの受付
Delivered:受信側サーバによるメール受付
Bounces:バウンス
Deferred:遅延
Drops:送信リクエストの破棄
Spam Reports:迷惑メール報告
Unsubscribes、Group Unsubscribes、Group Resubscribes:配信停止または配信停止の解除
Opens:メールの開封
Clicks:リンクのクリック
Parse:Inbound Parse Webhookでのメール受信

https://sendgrid.kke.co.jp/blog/?p=1273

7日で消えないようにするには、自分で用意したサーバにpost(EventWebhook)してもらうことで対応できる。 https://sendgrid.kke.co.jp/docs/User_Manual_JP/email_activity.html