Laravel-如何向SES添加自定义数据反弹有效载荷

时间:2018-06-27 02:57:27

标签: laravel amazon-ses

这是电子邮件反弹时AWS给出的通常有效载荷。

{
    "notificationType": "Bounce",
    "bounce": {
        "bounceType": "Transient",
        "bounceSubType": "General",
        "bouncedRecipients": [{
            "emailAddress": "John Doe <john.doe@blablabla.com>"
        }],
        "timestamp": "2018-06-20T00:08:33.000Z",
        "feedbackId": "010199641a7787f4-b716a44c-87fd-b4e8-aac6-9f3addafbf8e-000000"
    },
    "mail": {
        "timestamp": "2018-06-20T00:08:29.000Z",
        "source": "no.reply@mysite.com",
        "sourceArn": "arn:aws:ses:us-west-2:112348978975:identity/no.reply@mysite.com",
        "sourceIp": "72.121.95.229",
        "sendingAccountId": "156123967765",
        "messageId": "0101016c8a81e492-57779c7b-9175-4ab4-a993-c323431c98d1-000000",
        "destination": ["John Doe <john.doe@blablabla.com>"]
    }
}

我想要的是添加自定义属性到回弹有效载荷。例如

{"module": "reset_password"}

我发现this post可以使用message tag

如何使用Laravel 5.6设置此标签?

2 个答案:

答案 0 :(得分:0)

found the doc here,然后将自定义属性添加到标题中。

\Mail::send([], [], function ($email) use ($recipient) {
    $email->getHeaders()->addTextHeader('X-SES-MESSAGE-TAGS', 'module=auth,submodule=reset_password');
    $email->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
    $email->to($recipient);
    $email->subject('Test SES subject');
    $email->setBody('This is a test content', 'text/html');
});

,并且必须确保回弹有效载荷包括原始标头。

然后在网络挂钩中

$message_raw = \Aws\Sns\Message::fromRawPostData();
$message = json_decode($message_raw['Message'],1);
if (isset($message['mail']['headers'])) {
    $headers = collect($message['mail']['headers']);
    $tags = $headers->where('name', 'X-SES-MESSAGE-TAGS')->first();
    if ($tags) {
        $tags_arr = explode(',', $tags['value']);
        $tags = [];
        foreach ($tags_arr as $item) {
            list($key, $value) = explode('=', $item);
            $tags[$key] = $value;
        }
    }
}

$model->module = $tags['module']; // output auth
$model->submodule = $tags['submodule']; // output reset_password

答案 1 :(得分:0)

对于原始电子邮件

Mail::raw('content mail', function (\Illuminate\Mail\Message $message) use ($recipient) {
    $message->getSwiftMessage()->getHeaders()->addTextHeader('X-SES-MESSAGE-TAGS', 'my_tag=custom');
    $message->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
        ->to($recipient)
        ->subject('Hello world !');
});

用于可邮寄工作

$this->withSwiftMessage(function (\Swift_Message $message) {
     $message->getHeaders()->addTextHeader('X-SES-MESSAGE-TAGS', 'for_user=extra,email_type');
});