筆記: AWS SES 如何設定 Webhook ?
2025-01-09
AWS SES (Amazon Simple Email Service) 並無 Webhook 功能
若要獲得 SES 的寄信、退信、開信...的通知, 可使用 AWS SNS (Amazon Simple Notification Service) 來達成 Webhook 相同功能。
1.Amazon SNS 設定
新增一個 "主題 Topic"

在 "訂閱 Subscription 中,"建立訂閱"
"主題 ARN" 點下去,就可以選擇剛剛新增的 "SES_Hook"
"端點" 輸入自己的網址
例如 https://hello.abc.tw/misc/ses_hook.php
在自己的網站中放 ses_hook.php
php 內容為:
<?php
$content = file_get_contents("php://input");
$data = json_decode($content, true);
ob_start();
ob_implicit_flush(0);
var_dump($data);
$txt=ob_get_contents();
ob_end_clean();
if ( $fd=fopen('ses_log.txt',"a") ) {
fwrite($fd, "$txt\n\n");
fclose($fd);
}
http_response_code(200);
echo 'ok';
?>
按下 請求確認

ses_hook.php 會收到如下的訊息:
array(10) {
["Type"]=>
string(24) "SubscriptionConfirmation"
["MessageId"]=>
string(36) "97a14f33-15ad-4f0a-9ed5-29c9ca1032b6"
["Token"]=>
string(224) "2336412f37fb687f5d51e6e2425a8a5871498ba6b0a77f941......"
["TopicArn"]=>
string(43) "arn:aws:sns:us-east-1:601838488631:SNS_Hook"
["Message"]=>
string(164) "You have chosen to subscribe to the topic arn:aws:sns:us-east-1:601838488631:SNS_Hook.
To confirm the subscription, visit the SubscribeURL included in this message."
["SubscribeURL"]=>
string(347) "https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-east-1:601838488631:SNS_Hook&Token=2336412f37fb687f5d5425a8a58........."
["Timestamp"]=>
string(24) "2025-01-09T04:13:29.871Z"
["SignatureVersion"]=>
string(1) "1"
["Signature"]=>
string(344) "AaGNCaj6IVPd4rYJge68E/kkbjpMvhyO2DVOWoPls72YjzADtMGN2dwhaOTouqwsbk9OKZuIZK8QZeqJBN88F/5eq7lIZrHii2da5L6i....."
["SigningCertURL"]=>
string(98) "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-9c6465fa7f48f5cacd23014631ec1136.pem"
}
將 "SubscribeURL" 中的網址
貼到瀏覽器 即可完成認證, 如下圖

2. AWS SES 設定
如圖
選擇要接收 哪個信箱 的 寄件、退件通知

點 "通知 / 編輯"

選擇 SNS 主題 即可

上圖按 儲存 後,就會收到如下通知:
array(9) {
["Type"]=>
string(12) "Notification"
["MessageId"]=>
string(36) "2923b7c3-f0b3-5c41-a7cc-e83aa57587fd"
["TopicArn"]=>
string(43) "arn:aws:sns:us-east-1:601838488631:SES_Hook"
["Message"]=>
string(251) "{"notificationType":"AmazonSnsSubscriptionSucceeded","message":"You have successfully subscribed your Amazon SNS topic 'arn:aws:sns:us-east-1:601838488631:SES_Hook' to receive 'Delivery' notifications from Amazon SES for identity '[email protected]'."}"
["Timestamp"]=>
string(24) "2025-01-09T04:41:42.225Z"
["SignatureVersion"]=>
string(1) "1"
["Signature"]=>
string(344) "G12uPuFMUUZhWGSovRNLUJ5UZUAX6xWJsNVYPpdTyC41kOM8KeYzzu18p8EzTFw5/RsusKFBJbKhgPxE+hQLowtkhXZcMBHqJ7uKYPfv3VmRFDKvs2/iYAkOqw4sPVMD405DMfpHrn+A5qulUulocBVUadc6QKNV91XZ966oDA8pqVkShaOr9c8H1ctSc6qPDNeWwjrT+8e0YXjXKXYoXlN14k9f1MArtnrB3D4M2aCKeIBYpHm46oDTodXPjZ80cb8tgSMQ4IifYVbId1Dxdxx3ZH6fPOgnzks08QggRMyDSKqP0Z12et7bS7049zfKnDJ4DsVLqQz7JSKZdSwWDg=="
["SigningCertURL"]=>
string(98) "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-9c6465fa7f48f5cacd23014631ec1136.pem"
["UnsubscribeURL"]=>
string(152) "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:601838488631:SES_Hook:a91ffbc4-6fdf-488d-ad15-97adea7a3da2"
}
測試 SES 發信後能否收到通知
透過 SES 寄出一封信後
ses_hook.php 即可記錄到如下資訊:
array(9) {
["Type"]=>
string(12) "Notification"
["MessageId"]=>
string(36) "0d8c9371-0583-56b6-9f9a-cc886e36571f"
["TopicArn"]=>
string(43) "arn:aws:sns:us-east-1:601838488631:SES_Hook"
["Message"]=>
string(868) "{"notificationType":"Delivery","mail":{"timestamp":"2025-01-09T06:15:13.617Z","source":"[email protected]","sourceArn":"arn:aws:ses:us-east-1:601838488631:identity/[email protected]","sourceIp":"52.74.xx.xx","callerIdentity":"ses-smtp-user.20241118-174117","sendingAccountId":"601838488631","messageId":"0100019449b3e3d1-1d4880e2-55c1-4def-bab6-dec67cb5d346-000000","destination":["[email protected]"]},"delivery":{"timestamp":"2025-01-09T06:15:17.141Z","processingTimeMillis":3524,"recipients":["[email protected]"],"smtpResponse":"250 2.6.0 <0100019449b3e3d1-1d4880e2-55c1-4def-bab6-dec67cb5d346-000000@email.amazonses.com> [InternalId=10952166615481, Hostname=ME0P300MB1485.AUSP300.PROD.OUTLOOK.COM] 9889 bytes in 0.337, 28.584 KB/sec Queued mail for delivery -> 250 2.1.5","remoteMtaIp":"52.101.40.30","reportingMTA":"a48-30.smtp-out.amazonses.com"}}"
["Timestamp"]=>
string(24) "2025-01-09T06:15:17.194Z"
["SignatureVersion"]=>
string(1) "1"
["Signature"]=>
string(344) "Xx5LGHBouqVveJLswKpOQEuwkQ/KaZJEOSfFJGtTFGVC3/0OhO4CTCwyw..........."
["SigningCertURL"]=>
string(98) "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-9c6465fa7f48f5cacd23014631ec1136.pem"
["UnsubscribeURL"]=>
string(152) "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:601838488631:SES_Hook:a91ffbc4-..."
}
參考:
留言
相關文章