前面三节我们配置了SNS Notification, CloudWatch Dashboard,Firehose + S3
,本节我们将一起测试效果。
使用python实际发送几封邮件到邮箱(更改sender_email、recipient_email
):
import boto3
from botocore.exceptions import ClientError
ses_client = boto3.client('ses', region_name='us-east-1')
sender_email = "sender@example.com" # replace this with sender email address
recipient_email = "recipient@example.com" # replace this with receiver email address
for i in range(20):
try:
response = ses_client.send_email(
Source=sender_email,
Destination={
'ToAddresses': [recipient_email]
},
Message={
'Subject': {
'Data': 'Company Newsletter ' + str(i)
},
'Body': {
'Text': {
'Data': 'This is the content of the company newsletter.'
}
}
},
ConfigurationSetName="TechNewsConfigSet",
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID: ", response['MessageId'])
使用Mailbox Simulator
模拟 successful delivery, bounce, complaint
事件。
aws sesv2 send-email \
--from-email-address sender@example.com \
--destination ToAddresses=success@simulator.amazonses.com \
--content '{"Simple": {"Subject": {"Data": "Test Email to Success Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the success mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the success mailbox simulator.","Charset": "UTF-8"}}}}'
aws sesv2 send-email \
--from-email-address sender@example.com \
--destination ToAddresses=bounce@simulator.amazonses.com \
--content '{"Simple": {"Subject": {"Data": "Test Email to Bounce Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"}}}}'
例如:
aws sesv2 send-email --region us-east-1 --configuration-set-name TechNewsConfigSet\
--from-email-address abc@sesworkshop.kpingfan.com \
--destination ToAddresses=bounce@simulator.amazonses.com \
--content '{"Simple": {"Subject": {"Data": "Test Email to Bounce Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the bounce mailbox simulator.","Charset": "UTF-8"}}}}'
aws sesv2 send-email \
--from-email-address sender@example.com \
--destination ToAddresses=complaint@simulator.amazonses.com \
--content '{"Simple": {"Subject": {"Data": "Test Email to Complaint Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"}}}}'
例如:
aws sesv2 send-email --region us-east-1 --configuration-set-name TechNewsConfigSet\
--from-email-address abc@sesworkshop.kpingfan.com \
--destination ToAddresses=complaint@simulator.amazonses.com \
--content '{"Simple": {"Subject": {"Data": "Test Email to Complaint Mailbox Simulator","Charset": "UTF-8"},"Body": {"Text": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"},"Html": {"Data": "This is a test email to the complaint mailbox simulator.","Charset": "UTF-8"}}}}'
分别将上面的命令执行几次。
在CloudWatch Dashboard
,看到TechNewsConfigSet
几种事件的统计如下:
在配置的SNS notification
邮箱中,看到每种事件触发的邮件通知如下:
在S3中,看到Kinesis Firehose把对应事件的信息收集上来:
每条事件的信息如下:
{
"eventType": "Bounce",
"bounce": {
"feedbackId": "0100018b03e5bc13-01458d63-f7bd-4bb5-8e40-28bbb83f63ee-000000",
"bounceType": "Transient",
"bounceSubType": "General",
"bouncedRecipients": [
{
"emailAddress": "xxx@gamil.com",
"action": "failed",
"status": "5.3.0",
"diagnosticCode": "smtp; 511 sorry, no mailbox here by that name (#5.1.1 - chkuser)"
}
],
"timestamp": "2023-10-06T07:31:09.576Z",
"reportingMTA": "dns; a48-108.smtp-out.amazonses.com"
},
"mail": {
"timestamp": "2023-10-06T07:31:06.762Z",
"source": "jack@sesworkshop.kpingfan.com",
"sourceArn": "arn:aws:ses:us-east-1:145197526627:identity/sesworkshop.kpingfan.com",
"sendingAccountId": "145197526627",
"messageId": "0100018b03e5b18a-8761c405-a587-4121-9bff-5313bf98b841-000000",
"destination": [
"xxx@gamil.com"
],
"headersTruncated": false,
"headers": [
{
"name": "From",
"value": "jack@sesworkshop.kpingfan.com"
},
{
"name": "To",
"value": "xxx@gamil.com"
},
{
"name": "Subject",
"value": "Company Newsletter 4"
},
{
"name": "MIME-Version",
"value": "1.0"
},
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
},
{
"name": "Content-Transfer-Encoding",
"value": "7bit"
}
],
"commonHeaders": {
"from": [
"jack@sesworkshop.kpingfan.com"
],
"to": [
"xxx@gamil.com"
],
"messageId": "0100018b03e5b18a-8761c405-a587-4121-9bff-5313bf98b841-000000",
"subject": "Company Newsletter 4"
},
"tags": {
"ses:operation": [
"SendEmail"
],
"ses:configuration-set": [
"TechNewsConfigSet"
],
"ses:source-ip": [
"44.29.4.27"
],
"ses:from-domain": [
"sesworkshop.kpingfan.com"
],
"ses:caller-identity": [
"admin"
]
}
}
}
我们探讨了收集和处理SES事件的三种不同方法,这可以帮助我们获得有关邮件活动的宝贵见解、提高送达率并优化参与度。 每种方法都有其独特的优点,总结如下:
CloudWatch
:通过将 SES 指标记录到 CloudWatch,可以创建自定义控制面板、设置警报。 CloudWatch 能够监控和分析电子邮件活动的绩效,并提供用于诊断和响应问题的工具。 此方法非常适合需要监控和分析电子邮件指标以优化电子邮件参与度和送达率的场景。