Amazon Simple Email Service
欢迎使用 Amazon Simple Email Service (Amazon SES) 开发人员指南。Amazon SES 是一个简单而经济高效的电子邮件平台,您可以通过该平台使用您自己的电子邮件地址和域发送和接收电子邮件。
例如,您可以发送营销电子邮件 (如特别优惠)、事务电子邮件 (如订单确认) 以及新闻通讯和系统通知等其他类型的通信信息。在使用 Amazon SES 接收邮件时,您可以开发软件解决方案,例如电子邮件自动回复程序、电子邮件取消订阅系统以及根据传入的电子邮件生成客户支持服务单的应用程序。
发送限制
使用 SES 功能之前首先检查发送限制 SES Home -> Email Sending -> Sending Statistics
Your Amazon SES Sending Limits
Below are the latest statistics and metrics related to your Amazon SES Usage.
Sending Quota: send 200 emails per 24 hour period
Quota Used: 1% as of 2020-01-17 11:36 UTC+8
Max Send Rate: 1 email/second
Last updated: 2020-01-17 11:36 UTC+8
显然,SES 的初始限制是满足不了应用需求的,我们需要向 AWS 申请提升这些限制
Additional Information -> Request Increased Sending Limits
应用示例
创建 Policy,命名为
AppInEC2AccessSES{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "ses:*", "Resource": "*" } ] }将 Policy 附加到 EC2 关联的 IAM Role 上
编写应用程序代码
require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException; // Create an SesClient. Change the value of the region parameter if you're // using an AWS Region other than US West (Oregon). Change the value of the // profile parameter if you want to use a profile in your credentials file // other than the default. $SesClient = new SesClient([ 'version' => '2010-12-01', 'region' => 'us-east-1' ]); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $sender_email = 'sender@example.com'; // Replace these sample addresses with the addresses of your recipients. If // your account is still in the sandbox, these addresses must be verified. $recipient_emails = ['recipient1@example.com','recipient2@example.com']; // Specify a configuration set. If you do not want to use a configuration // set, comment the following variable, and the // 'ConfigurationSetName' => $configuration_set argument below. $configuration_set = 'ConfigSet'; $subject = 'Amazon SES test (AWS SDK for PHP)'; $plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ; $html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'. '<p>This email was sent with <a href="https://aws.amazon.com/ses/">'. 'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'. 'AWS SDK for PHP</a>.</p>'; $char_set = 'UTF-8'; try { $result = $SesClient->sendEmail([ 'Destination' => [ 'ToAddresses' => $recipient_emails, ], 'ReplyToAddresses' => [$sender_email], 'Source' => $sender_email, 'Message' => [ 'Body' => [ 'Html' => [ 'Charset' => $char_set, 'Data' => $html_body, ], 'Text' => [ 'Charset' => $char_set, 'Data' => $plaintext_body, ], ], 'Subject' => [ 'Charset' => $char_set, 'Data' => $subject, ], ], // If you aren't using a configuration set, comment or delete the // following line 'ConfigurationSetName' => $configuration_set, ]); $messageId = $result['MessageId']; echo("Email sent! Message ID: $messageId"."\n"); } catch (AwsException $e) { echo $e->getMessage(); echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n"); echo "\n"; }
可能的异常
异常信息
AWS HTTP error: Client error: `POST https://email.us-west-2.amazonaws.com` resulted in a `403 Forbidden` response: <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/"> <Error> <Type>Sender</Type> <Code>SignatureDo (truncated...) SignatureDoesNotMatch (client): Signature expired: 20181107T003842Z is now earlier than 20181107T005054Z (20181107T005554Z - 5 min.) - <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/"> <Error> <Type>Sender</Type> <Code>SignatureDoesNotMatch</Code> <Message>Signature expired: 20181107T003842Z is now earlier than 20181107T005054Z (20181107T005554Z - 5 min.)</Message> </Error> <RequestId>xxxxxxx-xxxx-xxxx-xxxx-13bf5130fec1</RequestId> </ErrorResponse>