LoginSignup
4

More than 5 years have passed since last update.

SendGrid の bounce や invalid に入ったメールアドレスを PHP アプリケーションから削除する

Last updated at Posted at 2015-09-16

課題

SendGrid API を扱う公式ライブラリとして sendgrid/sendgridsendgrid/smtpapi があるが、いずれも Web API の各種 Endpoint に対応していない.

前提

sendgrid-php v3.2.0 で確認.

解決策

SendGrid のインスタンスメソッド postRequest() が public に叩けるので、ここに任意の Web API エンドポイントを直接指定してやれば良い.

bounceリストに入ったアドレスを削除

$endpoint = "/api/bounces.delete.json";
$sg = new SendGrid("<api_user>", "<api_key>");

$form = [
    "api_user" => "<api_user>",
    "api_key" => "<api_key>",
    "email" => "<bounceから削除したいメールアドレス>"
];
$sg->postRequest($endpoint, $form)

invalidリストに入ったアドレスを削除

$endpoint/api/invalidemails.delete.json にすれば OK.

別解

  • SendGrid PHP Libraryなるものもあったが、試してない.
  • リクエスト自体はわりと単純なので、この用途だけなら、あえて SendGrid インスタンスを介さなくても良いくらいかも.

参考

補足(おまけ)

習作として、composer install できるライブラリにしてみた.

cu/sendgrid

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4