2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel+SESでメールを送信する

Posted at

結論

以下の手順で実装しよう🍄

  1. AWSコンソールでアクセスキーとシークレットを取得
  2. 1で取得した値をプロジェクトの環境変数に設定
  3. 2で設定した環境変数をサードパーティサービスの認証情報としてconfigで設定

具体的な手順

1. 認証情報取得

  • 下記手順で作成したAccess key IDとSecret access keyを控えておく

    IAM > ユーザー > (SES権限のある任意のユーザー) > 認証情報 > アクセスキーの作成

2. 認証情報を環境変数に設定

.env
SES_KEY=XXXXXXXXXXXXXXXXXXXX  # 1で取得したAccess key ID
SES_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  # 1で取得したSecret access key
SES_REGION=us-east-1  # SESのリージョン

3. 環境変数をサードパーティサービスの認証情報として設定

config/services.php
'ses' => [
    'key' => env('SES_KEY'),
    'secret' => env('SES_SECRET'),
    'region' => env('SES_REGION', 'us-east-1'),
],

問題発生時の対処方法

1. リージョンが違う

  • SESは2020年7月まで東京リージョンになかった
  • そのため、既存システムにおいては他のサービスと異なったリージョンを使っている場合がありがち

エラーログ例

production.ERROR: exception 'Aws\Ses\Exception\SesException' with message 'Error executing "SendRawEmail" on "https://email.ap-northeast-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://email.ap-northeast-1.amazonaws.com` resulted in a `400 Bad Request` response:
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
    <Type>Sender</Type>
    <Code>MessageReje (truncated...)
 MessageRejected (client): Email address is not verified. The following identities failed the check in region AP-NORTHEAST-1: hogehoge@fuga.com, send@piyo.site, PIYO <send@piyo.site> - <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
    <Type>Sender</Type>
    <Code>MessageRejected</Code>
    <Message>Email address is not verified. The following identities failed the check in region AP-NORTHEAST-1: hoge@fuga.com, send@piyo.site, PIYO &lt;send@piyo.site&gt;</Message>
  </Error>
  <RequestId>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</RequestId>
</ErrorResponse>

対処方法

  • 'region' => env('SES_REGION', 'us-east-1'),で任意のリージョンを指定する

2. 認証に失敗する

  • 認証情報はSES > SMTP Settings > Create My SMTP Credentialsからも生成できる
  • しかし、設定しなければならないのは先述の手順1の通り、IAMで生成したAccess key IDとSecret access key

エラーログ例

production.ERROR: exception 'Aws\Ses\Exception\SesException' with message 'Error executing "SendRawEmail" on "https://email.us-east-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://email.us-east-1.amazonaws.com` resulted in a `403 Forbidden` response:
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
    <Type>Sender</Type>
    <Code>SignatureDo (truncated...)
 SignatureDoesNotMatch (client): The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details. - <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
    <Type>Sender</Type>
    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
  </Error>
  <RequestId>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</RequestId>
</ErrorResponse>

対処方法

  • 手順1の通り、IAMで生成した認証情報を利用しよう
2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?