LoginSignup
2
0

More than 5 years have passed since last update.

PHPでAWS SESから(添付ファイル付き)メッセージ送信をしようとしたらハマった

Last updated at Posted at 2018-05-09

まず、 この記事にめっちゃめちゃお世話になりました。。。。多謝でございます!
https://qiita.com/kitanoow/items/2a81d9c4571656a31a2f

その上で、自分がハマった点をメモ。

前提

  • CakePHPを利用して、To/BCC指定でファイル添付メールを送りたい
  • 「受取人名」「差出人名」を指定したい
  • 事情があり、 EmailTransport1は利用しない
  • sendRawEmail()を直接的に利用する

何にハマったの

  • SES APIのパラメータ Destination を指定するだけだとうまく行かなかった
  • APIパラメータとしてDestination と Destinations がそれぞれ別に存在し、使い方が違うことに気づいていなかった

Destination を指定するだけだとうまく行かなかった

RawMessage.Dataに、ヘッダー情報を含める必要がある。

  • subject
  • to
  • cc
  • bcc

など。
MIME-Typeなどを指定したのみで2、 宛先情報や差出人情報といった(SDK的には別の箇所からも指定される)内容の記述を忘れており、そのために期待通りの動作をしていなかった。

最終的には、

$content = $headersToString($email->getHeaders()) .
      "\r\n\r\n" .
      $encodedContent .
      "\r\n\r\n\r\n";
$this->client->sendRawEmail([
    'Source' =>  'hogehoge<example@email.com>';
    'RawMessage' => ['Data' => $content]
]);

みたいな感じになるイメージ。

DestinationとDestinations
これらは別物。
前者は、Amazon風に言えば "フォーマット済みの"パラメータ指定方法となるだろうか。後者は「生のアドレスリスト」となる。

image.png
(https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendemail)

image.png

(https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendrawemail)

すなわち、以下のsendの書き方は2つとも同じような働きとなる。

$headers = [
    'To' => ["main-no-atesaki<{$toAddress1}>", "sub-no-atesaki<{$toAddress2}>"],
    'Bcc' => [$bccAddress],
];
// いい感じに $encodedContentを組み立てる

$this->client->sendRawEmail([
    'Source' => $from,
    'Destinations' => [
         $toAddress1,
         $toAddress2,
         $bccAddress,
    ],
    'RawMessage' => ['Data' => $encodedContent]
]);
$this->client->sendRawEmail([
    'Source' => $from,
    'Destination' => [
        'ToAddresses' => [$toAddress1, $toAddress2],
        'BccAddresses' => [$bccAddress],
    ],
    'RawMessage' => ['Data' => $encodedContent]
]);

ただ、

'Destinations' => [
        'ToAddresses' => 'email@example.com',
]

みたいな書き方も許容らしい。

'Destinations' => [
        'ToAddresses' => ['email@example.com'],
]

がだめで、コチラは

Found 1 error while validating the input provided for the SendRawEmail operation:[Destinations][ToAddresses] must be a string or an object that implements __toString(). Found array(1)

というエラーを吐き出された。
最初にこの書き方をしていてハマってしまい、パラメータ名が(それ自体は適格ということで)違っていることに気づかなかった・・・

みたいな話のメモでした!

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