使用Amazon CLI的 Amazon SES 示例 - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用Amazon CLI的 Amazon SES 示例

以下代码示例演示了如何通过将 Amazon Command Line Interface与 Amazon SES 结合使用,来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何使用 delete-identity

Amazon CLI

删除身份

以下示例使用 delete-identity 命令从通过 Amazon SES 验证的身份列表中删除身份:

aws ses delete-identity --identity user@example.com

有关已验证身份的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 DeleteIdentity

以下代码示例演示了如何使用 get-identity-dkim-attributes

Amazon CLI

获取身份列表的 Amazon SES Easy DKIM 属性

以下示例使用 get-identity-dkim-attributes 命令检索身份列表的 Amazon SES Easy DKIM 属性:

aws ses get-identity-dkim-attributes --identities "example.com" "user@example.com"

输出:

{ "DkimAttributes": { "example.com": { "DkimTokens": [ "EXAMPLEjcs5xoyqytjsotsijas7236gr", "EXAMPLEjr76cvoc6mysspnioorxsn6ep", "EXAMPLEkbmkqkhlm2lyz77ppkulerm4k" ], "DkimEnabled": true, "DkimVerificationStatus": "Success" }, "user@example.com": { "DkimEnabled": false, "DkimVerificationStatus": "NotStarted" } } }

如果调用此命令时,列表包含从未提交验证的身份,则该身份将不会出现在输出中。

有关 Easy DKIM 的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“Amazon SES 中的 Easy DKIM”。

以下代码示例演示了如何使用 get-identity-notification-attributes

Amazon CLI

获取身份列表的 Amazon SES 通知属性

以下示例使用 get-identity-notification-attributes 命令检索身份列表的 Amazon SES 通知属性:

aws ses get-identity-notification-attributes --identities "user1@example.com" "user2@example.com"

输出:

{ "NotificationAttributes": { "user1@example.com": { "ForwardingEnabled": false, "ComplaintTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic", "BounceTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic", "DeliveryTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic" }, "user2@example.com": { "ForwardingEnabled": true } } }

此命令将返回电子邮件反馈转发的状态,如果适用,还将返回向其发送退信、投诉和送达通知的 Amazon SNS 主题的 Amazon 资源名称(ARN)。

如果调用此命令时,列表包含从未提交验证的身份,则该身份将不会出现在输出中。

有关通知的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES 的通知”。

以下代码示例演示了如何使用 get-identity-verification-attributes

Amazon CLI

获取身份列表的 Amazon SES 验证状态

以下示例使用 get-identity-verification-attributes 命令检索身份列表的 Amazon SES 验证状态:

aws ses get-identity-verification-attributes --identities "user1@example.com" "user2@example.com"

输出:

{ "VerificationAttributes": { "user1@example.com": { "VerificationStatus": "Success" }, "user2@example.com": { "VerificationStatus": "Pending" } } }

如果调用此命令时,列表包含从未提交验证的身份,则该身份将不会出现在输出中。

有关已验证身份的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

以下代码示例演示了如何使用 get-send-quota

Amazon CLI

获取 Amazon SES 发送限制

以下示例使用 get-send-quota 命令返回 Amazon SES 发送限制:

aws ses get-send-quota

输出:

{ "Max24HourSend": 200.0, "SentLast24Hours": 1.0, "MaxSendRate": 1.0 }

Max24HourSend 是发送配额,即,您在 24 小时之内可发送的最大电子邮件数量。发送配额反映一个滚动的时段。每当您尝试发送电子邮件时,Amazon SES 都会检查您在过去 24 小时内发送的电子邮件数量。只要您发送电子邮件总数小于您的配额,发送请求就会被接受,并发送您的电子邮件。

SentLast24Hours 是您在过去 24 小时内已发送的电子邮件数量。

MaxSendRate 是您每秒最多可发送的电子邮件数量。

请注意,发送限制基于收件人而不是消息。例如,一封包含 10 个收件人的电子邮件占用 10 份发送配额。

有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“管理您的 Amazon SES 发送限制”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 GetSendQuota

以下代码示例演示了如何使用 get-send-statistics

Amazon CLI

获取 Amazon SES 发送统计信息

以下示例使用 get-send-statistics 命令返回 Amazon SES 发送统计信息

aws ses get-send-statistics

输出:

{ "SendDataPoints": [ { "Complaints": 0, "Timestamp": "2013-06-12T19:32:00Z", "DeliveryAttempts": 2, "Bounces": 0, "Rejects": 0 }, { "Complaints": 0, "Timestamp": "2013-06-12T00:47:00Z", "DeliveryAttempts": 1, "Bounces": 0, "Rejects": 0 } ] }

结果是数据点的列表,表示过去两周的发送活动。列表中的每个数据点都包含 15 分钟间隔的统计信息。

在此示例中,只有两个数据点,因为用户在过去两周内发送的唯一电子邮件是在两个 15 分钟间隔内发送的。

有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“监控您的 Amazon SES 使用情况统计信息”。

  • 有关 API 的详细信息,请参阅《Amazon CLI 命令参考》中的 GetSendStatistics

以下代码示例演示了如何使用 list-identities

Amazon CLI

列出特定 Amazon 账户的所有身份(电子邮件地址和域)

以下示例使用 list-identities 命令列出已提交到 Amazon SES 进行验证的所有身份:

aws ses list-identities

输出:

{ "Identities": [ "user@example.com", "example.com" ] }

返回的列表包含所有身份,无论验证状态如何(已验证、待验证、失败等)。

在此示例中,由于未指定 identity-type 参数,因此返回了电子邮件地址 域。

有关验证的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 ListIdentities

以下代码示例演示了如何使用 send-email

Amazon CLI

使用 Amazon SES 发送格式化的电子邮件

以下示例使用 send-email 命令发送格式化的电子邮件:

aws ses send-email --from sender@example.com --destination file://destination.json --message file://message.json

输出:

{ "MessageId": "EXAMPLEf3a5efcd1-51adec81-d2a4-4e3f-9fe2-5d85c1b23783-000000" }

目标和消息是 JSON 数据结构,保存在当前目录下的 .json 文件中。这些文件如下所示:

destination.json:

{ "ToAddresses": ["recipient1@example.com", "recipient2@example.com"], "CcAddresses": ["recipient3@example.com"], "BccAddresses": [] }

message.json:

{ "Subject": { "Data": "Test email sent using the AWS CLI", "Charset": "UTF-8" }, "Body": { "Text": { "Data": "This is the message body in text format.", "Charset": "UTF-8" }, "Html": { "Data": "This message body contains HTML formatting. It can, for example, contain links like this one: <a class=\"ulink\" href=\"http://docs.aws.amazon.com/ses/latest/DeveloperGuide\" target=\"_blank\">Amazon SES Developer Guide</a>.", "Charset": "UTF-8" } } }

请将发件人和收件人的电子邮件地址替换为您要使用的电子邮件地址。请注意,发件人的电子邮件地址必须已通过 Amazon SES 验证。在您获得 Amazon SES 的生产访问权限之前,您还必须验证每个收件人的电子邮件地址,除非收件人是 Amazon SES 邮箱模拟器。有关验证的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

输出中的消息 ID 表示 send-email 调用成功。

如果您没有收到电子邮件,请检查垃圾邮件。

有关发送格式化电子邮件的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES API 发送格式化的电子邮件”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 SendEmail

以下代码示例演示了如何使用 send-raw-email

Amazon CLI

使用 Amazon SES 发送原始电子邮件

以下示例使用 send-raw-email 命令发送带有 TXT 附件的电子邮件:

aws ses send-raw-email --raw-message file://message.json

输出:

{ "MessageId": "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000" }

原始消息是一个 JSON 数据结构,保存在当前目录下名为 message.json 的文件中。其中包含以下内容:

{ "Data": "From: sender@example.com\nTo: recipient@example.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename=\"attachment.txt\"\n\nThis is the text in the attachment.\n\n--NextPart--" }

如您所见,“Data”是一个长字符串,包含 MIME 格式的全部原始电子邮件内容,其中包括一个名为 attachment.txt 的附件。

请将 sender@example.com 和 recipient@example.com 替换为您要使用的地址。请注意,发件人的电子邮件地址必须已通过 Amazon SES 验证。在您获得 Amazon SES 的生产访问权限之前,您还必须验证收件人的电子邮件地址,除非收件人是 Amazon SES 邮箱模拟器。有关验证的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

输出中的消息 ID 表示 send-raw-email 调用成功。

如果您没有收到电子邮件,请检查垃圾邮件。

有关发送原始电子邮件的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES API 发送原始电子邮件”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 SendRawEmail

以下代码示例演示了如何使用 set-identity-dkim-enabled

Amazon CLI

为经 Amazon SES 验证的身份启用或禁用 Easy DKIM

以下示例使用 set-identity-dkim-enabled 命令禁用已验证电子邮件地址的 DKIM:

aws ses set-identity-dkim-enabled --identity user@example.com --no-dkim-enabled

有关 Easy DKIM 的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“Amazon SES 中的 Easy DKIM”。

以下代码示例演示了如何使用 set-identity-feedback-forwarding-enabled

Amazon CLI

为经 Amazon SES 验证的身份启用或禁用退信和投诉电子邮件反馈转发

以下示例使用 set-identity-feedback-forwarding-enabled 命令使经验证的电子邮件地址能够通过电子邮件接收退信和投诉通知:

aws ses set-identity-feedback-forwarding-enabled --identity user@example.com --forwarding-enabled

您必须通过 Amazon SNS 或电子邮件反馈转发接收退信和投诉通知,因此,只有在为退信和投诉通知选择 Amazon SNS 主题时,才可以禁用电子邮件反馈转发。

有关通知的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES 的通知”。

以下代码示例演示了如何使用 set-identity-notification-topic

Amazon CLI

设置 Amazon SNS 主题,Amazon SES 将向其发布已验证身份的退信、投诉和/或送达通知

以下示例使用 set-identity-notification-topic 命令指定经验证的电子邮件地址将接收退信通知的 Amazon SNS 主题:

aws ses set-identity-notification-topic --identity user@example.com --notification-type Bounce --sns-topic arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic

有关通知的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES 的通知”。

以下代码示例演示了如何使用 verify-domain-dkim

Amazon CLI

生成经验证的域的 DKIM 令牌,以便使用 Amazon SES 进行 DKIM 签名

以下示例使用 verify-domain-dkim 命令为已通过 Amazon SES 验证的域生成 DKIM 令牌:

aws ses verify-domain-dkim --domain example.com

输出:

{ "DkimTokens": [ "EXAMPLEq76owjnks3lnluwg65scbemvw", "EXAMPLEi3dnsj67hstzaj673klariwx2", "EXAMPLEwfbtcukvimehexktmdtaz6naj" ] }

要设置 DKIM,您必须使用返回的 DKIM 令牌更新域名的 DNS 设置,使其具有指向 Amazon SES 托管的 DKIM 公钥的 CNAME 记录。有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“Amazon SES 中的 Easy DKIM”。

  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 VerifyDomainDkim

以下代码示例演示了如何使用 verify-domain-identity

Amazon CLI

使用 Amazon SES 验证域

以下示例使用 verify-domain-identity 命令验证域:

aws ses verify-domain-identity --domain example.com

输出:

{ "VerificationToken": "eoEmxw+YaYhb3h3iVJHuXMJXqeu1q1/wwmvjuEXAMPLE" }

要完成域验证,您必须在域的 DNS 设置中添加一条包含返回的验证令牌的 TXT 记录。有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证域”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 VerifyDomainIdentity

以下代码示例演示了如何使用 verify-email-identity

Amazon CLI

使用 Amazon SES 验证电子邮件地址

以下示例使用 verify-email-identity 命令验证电子邮件地址:

aws ses verify-email-identity --email-address user@example.com

在使用 Amazon SES 发送电子邮件之前,您必须验证从中发送电子邮件的地址或域,以证明您拥有该地址或域。如果您尚未获得生产访问权限,则还需要验证所有收件电子邮件地址,Amazon SES 邮箱模拟器提供的电子邮件地址除外。

调用 verify-email-identity 后,该电子邮件地址将收到一封验证电子邮件。用户必须单击此电子邮件中的链接才能完成验证过程。

有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址”。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 VerifyEmailIdentity