五步利用AWS SNS自动截取SES邮箱返回错误数据

背景:采用亚马逊的SES邮件服务器,群发邮件,当遇到错误邮箱的时候,亚马逊会返回一个信息,告诉我这个邮箱bounce了。

后来采用SNS的提示服务器,只搞对了将bounce信息发送给我的邮箱。
今天月小升下定决心,让一切自动化,让bounce信息弹出到一个接口,然后用接口来进行自动处理错误邮件

核心接口说明官方文件

https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html

(一)建立自己服务器的认证文件,同时这个文件也是个截取最终错误email得文件
<pre>http://java-er.com/test.php

服务器java-er.com上建立一个php文件,比如http://java-er.com/test.php

亚马逊返回的日志只能写入在txt文件里,因为网络里是无法看到的。接口调试都这样,一遍一遍向服务器覆盖修改的php文件。

		$post = $GLOBALS['HTTP_RAW_POST_DATA'];
 
		var_dump($post);
 
		$data = $post."A\n";
		//json_encode($_REQUEST);
		//echo $data;
		$fp = fopen("/test/aws.txt", "a+") or die("Unable to open file!");
		$len = fwrite($fp, $data);
		fclose($fp);

(二) SNS建立认证
去亚马逊的SNS里,建立一个http的终点,然后进行认证操作

然后认证

点击Request confirmations

返回以下信息到/test/aws.txt

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "0660996e-af81-4b7a-a52f-46f8166b4622",
  "Token" : "2336412f37fb687f5d51e6e241da92fd7c2394a8ebcc52fe9174be09b3d93fdb95b8fd7ef30c6eb2d434a7893043437d12cbde30e52b759935d956ada8e32d3fac00ab2f1f193045a6cf5359081a4e36f76b4e730cca4f53a521a35d6780a95
21c1fb118e1fd6270c761ce9b4ddcff09",
  "TopicArn" : "arn:aws:sns:us-west-2:342718244599:BounceMail",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:342718244599:BounceMail.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:342718244599:BounceMail&Token=2336412f37fb687f5d51e6e241da92fd7c2394a8ebcc52fe9174be09b3d
93fdb95b8fd7ef30c6eb2d434a7893043437d12cbde30e52b759935d956ada8e32d3fac00ab2f1f193045a6cf5359081a4e36f76b4e730cca4f53a521a35d6780a9521c1fb118e1fd6270c761ce9b4ddcff09",
  "Timestamp" : "2018-09-05T03:46:55.504Z",
  "SignatureVersion" : "1",
  "Signature" : "By1lktx95JbM5tzXM4HFUjdZ1VRQYb9MfaA5BylXzQnSk5Uqo0+0djo4qAFo/6L/aac/vYSwsJuaY34EAOEr/Y487nb7FGR7JLe0MmtDbYI0gU4R8y7+dIudDIkpRA2E23DXu5mgfOHrkciiheJcgE30JA+HexYVb2eP9IHdhYW4fDlwJV5w7kIuHc1
4VLshSfGohZMk0ItGU5+6EdfQQDSlA+DpFZOI4OaANZ6773CTIyiHI439PkLq4Ub8VICfzsXTG/3z8Rjl4ad7RAyMRYhQqk1qAhIoZ9x04q2ZoJgdDfiCL3RFDuFFxtpklM2S72a8h8m0i4KLPc4GPrykXA==",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem"
}

SubscribeURL 的那个一串url拷贝到浏览器,打开然后就激活了。

浏览器会返回如下的信息

<ConfirmSubscriptionResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<ConfirmSubscriptionResult>
<SubscriptionArn>
arn:aws:sns:us-west-2:34271228244599:BounceMail:d60f0128-7125-4c60-9bb1-14b7e951309c
</SubscriptionArn>
</ConfirmSubscriptionResult>
<ResponseMetadata>
<RequestId>52c44de8-9f64-59265-a32b-19e1cb12c5aa</RequestId>
</ResponseMetadata>
</ConfirmSubscriptionResponse>

52c44de8-9f64-59265-a32b-19e1cb12c5aa 这一串就是ID
认证就完成了

(三) 发送测试信息给接口
模拟器发送一条信息,进行bounce测试

aws返回的数据 都在我的日志文件/test/aws.txt

{
  "Type" : "Notification",
  "MessageId" : "b9088764-f57f-5c85-97db-d36da0249b71",
  "TopicArn" : "arn:aws:sns:us-west-2:342718244599:BounceMail",
  "Subject" : "Test3",
  "Message" : "wrong",
  "Timestamp" : "2018-09-05T03:52:59.823Z",
  "SignatureVersion" : "1",
  "Signature" : "oVepDnfQA7t1bQ8SVmKOvRFnlzcTwLeBeu9Ox+QUnlZWAlvRL5oOysw/spKfLzo
nHEVmPz9y0M8aknhb+jkRrfuJDfjRHmTcFUGL2+bcR2+iypLQHyNqZQ94dLgks9JrYl/TLjkmJKlkNZy
tcTmUvZuW5jImyNpc+zxsUiPClYMgtWnO8SXxTR+NnDsyuucJXw5lnTMAhXzPC+kIt35LrBV2CTNz647
rEK0pjpRAJB6UYykoZMrHwfgD6wSIe7NE+af8WYVWvY3ZLwBICBaDIe08+fXSmmMEdFjJIm3a7+dG60k
1ad3dfKKuVIvg4wXbrxNn8FfzqNaEve58VTdpmA==",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationServ
ice-eaea6120e66ea12e88dcd8bcbddca752.pem",
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&Su
bscriptionArn=arn:aws:sns:us-west-2:342718244599:BounceMail:d60f0128-7125-4c60-9
bb1-14b7e951309c",
  "MessageAttributes" : {
    "AWS.SNS.MOBILE.MPNS.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.BAIDU.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.MACOS_SANDBOX.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.GCM.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.MACOS.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.ADM.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.APNS_SANDBOX.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.APNS.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.APNS_VOIP_SANDBOX.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.APNS_VOIP.TTL" : {"Type":"String","Value":"1"},
    "AWS.SNS.MOBILE.WNS.TTL" : {"Type":"String","Value":"1"}
  }
}

(四)模拟aws的发送邮件测试信息给接口

https://docs.aws.amazon.com/ses/latest/DeveloperGuide/mailbox-simulator.html

bounce@simulator.amazonses.com 这个邮箱是模拟bounce的邮件的。用aws mail 发送一个任意邮件到这个地址,就会触发邮件bounce行为

aws发邮件代码

 
public function aws_sendMail($to, $title, $content) {
		header("content-type:text/html;charset=utf-8");
		$mail =new \Org\Util\PHPMailer;
		$mail->IsSMTP(); // 启用SMTP
		$mail->Host="email-smtp.us-west-2.amazonaws.com"; //smtp服务器的名称(这里以QQ邮箱为例)
		$mail->SMTPAuth = true; //启用smtp认证
		$mail->Username = "AKIAIYEUIG7L4MSCJIDA"; //你的邮箱名
		$mail->Password = "2+Uo5OT5eRGaDJyaVNfnzxSA" ; //邮箱密码
		$mail->From = "admin@java-er.com"; //发件人地址(也就是你的邮箱地址)
		$mail->FromName = 'Java-er'; //发件人姓名
		$mail->AddAddress($to,"");
		$mail->WordWrap = 50; //设置每行字符长度
		$mail->IsHTML(true); // 是否HTML格式邮件
		//$mail->CharSet=""; //设置邮件编码
		$mail->Subject =$title; //邮件主题
		$mail->Body = $content; //邮件内容
 
		// Enable TLS encryption over port 587
		$mail->SMTPSecure = 'tls';
		$mail->Port = 587;
 
		$mail->AltBody = "This is a text only body in a nonprofit HTML email client"; //邮件正文不支持HTML的备用显示
		//return($mail->Send());
 
		if(!$mail->send()) {
			echo "<hr />Email not sent. ".$to , $mail->ErrorInfo , PHP_EOL;
			return 'ERROR AWS:'.$mail->ErrorInfo;
		} else {
			echo "<hr />Email sent!".$to , PHP_EOL;
			return 'AWS OK';
		}
 
	}

然后系统的日志aws.txt日志返回如下数据

{
  "Type" : "Notification",
  "MessageId" : "5788255e-cbac-5723-95bf-7157903ca4ad",
  "TopicArn" : "arn:aws:sns:us-west-2:342718244599:BounceMail",
  "Message" : "{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Permanent\",\"bounceSubType\":\"General\",\"bou
ncedRecipients\":[{\"emailAddress\":\"bounce@simulator.amazonses.com\",\"action\":\"failed\",\"status\":\"5.1.1\",\"diagnost
icCode\":\"smtp; 550 5.1.1 user unknown\"}],\"timestamp\":\"2018-09-05T04:19:28.765Z\",\"feedbackId\":\"01010165a7f4e33f-b90
64c79-9e00-42bb-87df-835d12d919d5-000000\",\"remoteMtaIp\":\"207.171.163.188\",\"reportingMTA\":\"dsn; a27-55.smtp-out.us-we
st-2.amazonses.com\"},\"mail\":{\"timestamp\":\"2018-09-05T04:19:27.000Z\",\"source\":\"donotreply@leoptique.com\",\"sourceA
rn\":\"arn:aws:ses:us-west-2:342718244599:identity/leoptique.com\",\"sourceIp\":\"221.219.85.123\",\"sendingAccountId\":\"34
2718244599\",\"messageId\":\"01010165a7f4e013-f217704a-0d8f-4211-a89e-29f48d2739ae-000000\",\"destination\":[\"bounce@simula
tor.amazonses.com\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Received\",\"value\":\"from test.eye_admin.com ([22
1.219.85.123]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-2761973385) id PR7N0IbfHhmKnz7i3s1z for bounce@simu
lator.amazonses.com; Wed, 05 Sep 2018 04:19:27 +0000 (UTC)\"},{\"name\":\"DateWed, 5 Sep 2018 00\",\"value\":\"19:23 -0400: 
\"},{\"name\":\"Return-Path\",\"value\":\"donotreply@leoptique.com\"},{\"name\":\"To\",\"value\":\"bounce@simulator.amazonse
s.com\"},{\"name\":\"From\",\"value\":\"Leoptique <donotreply@leoptique.com>\"},{\"name\":\"Subject\",\"value\":\"Test\"},{\
"name\":\"Message-ID\",\"value\":\"<81abff671ab67ff6e862876bad93fa63@test.eye_admin.com>\"},{\"name\":\"X-Priority\",\"value
\":\"3\"},{\"name\":\"X-Mailer\",\"value\":\"PHPMailer 5.1 (phpmailer.sourceforge.net)\"},{\"name\":\"MIME-Version\",\"value
\":\"1.0\"},{\"name\":\"Content-Type\",\"value\":\"multipart/alternative; boundary=\\\"b1_81abff671ab67ff6e862876bad93fa63\\
\"\"}],\"commonHeaders\":{\"returnPath\":\"donotreply@leoptique.com\",\"from\":[\"Leoptique <donotreply@leoptique.com>\"],\"
to\":[\"bounce@simulator.amazonses.com\"],\"messageId\":\"<81abff671ab67ff6e862876bad93fa63@test.eye_admin.com>\",\"subject\
":\"Test\"}}}",
  "Timestamp" : "2018-09-05T04:19:28.793Z",
  "SignatureVersion" : "1",
  "Signature" : "TQuqaJtelaAUrO+mluX0na1RZl7GdrKCQxrc5vSixtBo+d1QtEvwGw8VLdT3LkCGhvpcz52EXLN6VZ0O2wnkEeIc2H/nSQXTWUnIknhioSB
G1Xp3civdEQE8N8z4PNfvzLFfRcMUwYp3R0jWVsiu1aG2vtumaHXgdsiblIlSfoPbh0uzU79lKy2t0Z3zpK7/YOALULJVFRctDkKiLS1DE095i09altkt2wl1DDx
6EQ+MQyrRpfKQXzuTgfi+e27rtM9Qu9llZHBvsSGjKMKw5xUZn3f5+OWqJscFerdxET6NpkwX2knaEPCGNSuDBv6DFjpSK/utmV9JxKXY2JmMdQ==",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-eaea6120e66ea12e88dcd8bcbddca752.pem",
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:34271824
4599:BounceMail:d60f0128-7125-4c60-9bb1-14b7e951309c"
}

(五) 提取我最想要的被bounce的 EMAIL信息
返回的数据解析,需要两次json_decode,才能提取email

$post = $GLOBALS['HTTP_RAW_POST_DATA'];
 
		var_dump($post);
 
 
		$arr = json_decode($post,true);
		$email = $arr['Message'];
 
		$x1 = json_decode($email,true);
 
		$email = $x1['bounce']['bouncedRecipients'][0]['emailAddress'];
 
 
 
		//$email = print_r($email, true);

顺便还学会了在日志文件打印数组的办法

$email = print_r($email, true);

得到邮箱,后续的处理就是插入数据的事情了。


This entry was posted in Linux, PHP, 国外主机 and tagged , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/aws-sns-ses-err-email/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

*

  

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

About Me

静水流深,水滴石穿