lo云中
In this article, I will explain how Amazon’s Simple Email Service (SES) is a better way to send email from your web application, and I’ll provide a roadmap for getting started.
在本文中,我将说明亚马逊的简单电子邮件服务(SES)如何是从Web应用程序发送电子邮件的更好方法,并且将提供入门指南。
Generally speaking, email on the internet is sent using SMTP servers. Web applications are no exception. But, as anyone who has dealt with large-scale email sending through their web application knows, using SMTP to send email can be a real pain.
一般来说,Internet上的电子邮件是使用SMTP服务器发送的。 Web应用程序也不例外。 但是,正如处理过通过其Web应用程序发送大规模电子邮件的任何人都知道的那样,使用SMTP发送电子邮件可能是一个真正的痛苦。
The easiest way to see how Amazon’s SES service improves upon the traditional methods of sending email is to compare SES to SMTP:
要了解Amazon的SES服务如何在传统的发送电子邮件方法上有所改进,最简单的方法是将SES与SMTP进行比较:
Feature | Traditional SMTP | Amazon SES |
Availability | Either you or your web host must maintain an SMTP server. If the server is down, your application cannot send email. | Unlike a traditional SMTP server that is a single point of failure, Amazon SES is distributed and designed to be highly available. |
Scalability | As your web application sends more and more email, you’ll need to upgrade your SMTP server. Eventually you’ll need multiple servers in a load-balancing or clustering configuration. | Amazon SES can easily handle the growth of your email volume as your application expands. No need to invest in servers, setup, or maintenance. You simply pay as you go for what you use. |
Cost | Sure, you could try and set up your own scalable, highly-available SMTP platform. All you’ll need is a few servers at multiple datacenters and two or three fulltime technicians to monitor and maintain the service. That’s in your budget, right? | You can use Amazon SES for free—up to 2,000 messages per day (you just pay for traffic). If you send more than that, you pay mere pennies to send thousands of emails. |
Deliverability | Major email providers strictly filter incoming mail. In order to achieve high delivery rates, you must be able and willing to jump through all kinds of technical hoops. | With SES, Amazon takes steps to ensure your messages are delivered. |
Tracking | SMTP server logs can tell you if your email was accept by the receiving ISP (have fun working with server logs to track delivery). | Amazon SES provides easy access to data about email delivery. Not only can you find out if your email is bouncing, but with SES you can see how many of your emails are being marked as spam or junk by recipients at major ISPs. |
特征 | 传统SMTP | 亚马逊SES |
可用性 | 您或您的Web主机必须维护SMTP服务器。 如果服务器已关闭,则您的应用程序无法发送电子邮件。 | 与单点故障的传统SMTP服务器不同,Amazon SES的分布和设计使其具有高可用性。 |
可扩展性 | 随着您的Web应用程序发送越来越多的电子邮件,您将需要升级SMTP服务器。 最终,您将需要在负载平衡或群集配置中的多个服务器。 | 随着应用程序扩展,Amazon SES可以轻松应对电子邮件量的增长。 无需投资于服务器,设置或维护。 您只需随用随付就可以使用。 |
成本 | 当然,您可以尝试建立自己的可扩展的,高度可用的SMTP平台。 您只需要在多个数据中心的几台服务器和两名或三名全职技术人员来监视和维护服务即可。 那是在您的预算中,对吧? | 您可以免费使用Amazon SES-每天多达2,000条消息(您只需为流量付费)。 如果您发送的邮件超过此数,则只需花几便士即可发送数千封电子邮件。 |
交付能力 | 主要的电子邮件提供商严格过滤传入的邮件。 为了实现高交付率,您必须并且愿意跳过各种技术难题。 | 借助SES,Amazon采取了确保您的消息传递的步骤。 |
追踪 | SMTP服务器日志可以告诉您您的电子邮件是否被接收ISP接受(使用服务器日志来跟踪传递很有趣)。 | Amazon SES可以轻松访问有关电子邮件传递的数据。 您不仅可以找出您的电子邮件是否被退回,而且还可以使用SES来查看主要ISP的收件人将多少电子邮件标记为垃圾邮件或垃圾邮件。 |
Now that you know how much better life can be using Amazon SES in your application, let’s dive into a few examples. We’ll be building off knowledge from a previous article titled “Getting Started with the Amazon AWS PHP SDK”.
现在您已经知道在应用程序中使用Amazon SES可以带来更好的生活,下面让我们来看一些示例。 我们将从以前的文章“ Amazon AWS PHP SDK入门 ”中积累知识。
Before you can work with any of the following examples, you must sign up for the Amazon SES service. To do so, simply sign in to your AWS account, visit the Amazon SES product page (http://aws.amazon.com/ses/), and click “Sign up now”.
在使用以下任何示例之前,您必须先注册Amazon SES服务。 为此,只需登录您的AWS账户,访问Amazon SES产品页面( http://aws.amazon.com/ses/ ),然后单击“立即注册”。
沙箱限制和电子邮件验证 (Sandbox Limitations and Email Verification)
The first thing you need to know about using Amazon SES is that, when you first sign up for the service, your account will be limited in functionality. This means two things:
关于使用Amazon SES的第一件事是,当您首次注册该服务时,您的帐户将受到功能限制。 这意味着两件事:
- You can only send from email addresses that you have verified. 您只能从已验证的电子邮件地址发送。
- You can only send to email addresses you have verified. 您只能发送到已验证的电子邮件地址。
This obviously won’t work for a production deployment, but it’s just fine for testing. So the first thing you’re going to need to do is verify a couple of email addresses. You can start the verification process with just a few lines of code:
这显然不适用于生产部署,但是对于测试来说就很好。 因此,您需要做的第一件事就是验证几个电子邮件地址。 您只需几行代码即可开始验证过程:
// You'll need the SDK Class for these examples.
// Learn more at: https://www.sitepoint.com/getting-started-with-the-aws-php-sdk/
require_once 'sdk.class.php';
// Create an instance of the SES class.
$email = new AmazonSES();
// Call the verification method using your own email address
$response = $email->verify_email_address('someemail@example.com');
print_r($response);
Simply running the code example above will initiate verification. You’ll receive an email at the address you submit with a coded verification link. Then just click the link and your email address will be verified.
只需运行上面的代码示例即可启动验证。 您会在提交的地址收到一封带有编码验证链接的电子邮件。 然后只需单击链接,您的电子邮件地址将被验证。
Repeat this process for any number of email addresses you’d like to send from or to during your testing.
对您想要在测试期间发送或发送的任意数量的电子邮件地址重复此过程。
发送您的第一封电子邮件 (Sending Your First Email)
Now that you have a verified email address or two, you can start sending email. This is extremely simple:
现在您已经拥有一两个经过验证的电子邮件地址,就可以开始发送电子邮件了。 这非常简单:
require_once 'sdk.class.php';
$email = new AmazonSES();
// Your verfified sending email address.
$sendFrom = 'someemail@example.com';
// The email address you're sending to
// (must be a verified email while your account
// is in the sandbox).
$sendTo = 'someemail@example.com';
// The message and body of your email:
// (Adding .date('c') to the subject
// makes it easy to track separate tests)
$messageSubject = 'My First AWS Email: '.date('c');
$messageBody = 'Hi there! This is my first message.';
// Create the request:
$response = $email->send_email(
$sendFrom,
array('ToAddresses' => $sendTo),
array(
'Subject.Data' => $messageSubject,
'Body.Text.Data' => $messageBody
)
);
// If your send fails,
// You'll be able to find the reason why
// the response information.
print_r($response);
That’s all there is to it!
这里的所有都是它的!
发送HTML电子邮件 (Sending HTML Email)
Adding HTML to your email makes it easy to add formatting and links that can make your email more effective. The PHP SDK makes it easy:
在电子邮件中添加HTML可以轻松添加格式和链接,从而使电子邮件更有效。 PHP SDK使操作变得简单:
require_once 'sdk.class.php';
$email = new AmazonSES();
$sendFrom = 'someemail@example.com';
$sendTo = 'someemail@example.com';
$messageSubject = 'My SES Email with HTML: '.date('c');
$messageBody = 'Hi there! This is a message with a link: http://www.sitepoint.com';
// You can specify an HTML alternative
// for your email which will allow you to
// add links, formatting, tables,
// and other html elements to your message:
$messageBodyHtml = 'Hi there! This is a message with a link: <a href="http://www.sitepoint.com" style="color: #D90000;">http://www.sitepoint.com</a>';
$response = $email->send_email(
$sendFrom,
array('ToAddresses' => $sendTo),
array(
'Subject.Data' => $messageSubject,
'Body.Text.Data' => $messageBody,
'Body.Html.Data' => $messageBodyHtml,
)
);
print_r($response);
更多进阶选项 (More Advanced Options)
In addition to the common task of sending formatted email, the SDK provides some additional methods that can be very useful:
除了发送格式化电子邮件的常见任务外,SDK还提供了一些其他非常有用的方法:
Send Raw Email – For more advanced functionality, the PHP SDK allows you to send a raw email message. This allows you to add multiple parts to your email, including attachments.
发送原始电子邮件 –为了获得更高级的功能,PHP SDK允许您发送原始电子邮件。 这使您可以将多个部分(包括附件)添加到电子邮件中。
List Verified Email Addresses – It can be handy to get a list of email addresses you have previously verified, and in some cases you might even want to us the “Deleted Verified Email Address” method to remove emails that should no longer be allowed to send from your account.
列出已验证的电子邮件地址 –可以方便地获取以前已验证的电子邮件地址列表,在某些情况下,您甚至可能希望我们使用“已删除的已验证电子邮件地址”方法来删除不应再允许发送的电子邮件从您的帐户。
Get Send Quota – Based on your email habits, your Amazon AWS account will place a limit on how many emails you can send per day. This limit can range from 1,000 emails up to a million or more per day.
获取发送配额 –根据您的电子邮件习惯,您的Amazon AWS账户将限制您每天可以发送的电子邮件数量。 该限制范围从每天1,000封电子邮件到每天一百万甚至更多。
Get Send Statistics – This powerful method allows you to track real-time statistics, including Successful Delivery Attempts, Rejected Messages, Bounces, and Complaints.
获取发送统计信息 –这种功能强大的方法使您可以跟踪实时统计信息,包括成功的传递尝试,已拒绝的邮件,退回和投诉。
离开沙盒 (Leaving the Sandbox)
Once you’re ready to go live with your new email service, you’ll need to request production access for sending email. A link to make this request can be found on the Amazon SES product page and requests are usually granted within a day or so.
一旦准备好使用新的电子邮件服务,就需要请求生产访问权限才能发送电子邮件。 可以在Amazon SES产品页面上找到发出此请求的链接,通常在一天左右的时间内就可以批准请求。
更多提示 (A Few More Tips)
Abuse – Amazon SES is not designed to make it easy to send unsolicited email to mailing lists you buy. If you send unwanted email using SES, you’ll be shut down very quickly.
滥用 – Amazon SES并非旨在轻松地将未经请求的电子邮件发送到您购买的邮件列表。 如果您使用SES发送不需要的电子邮件,则会很快关闭。
Quota – Amazon SES dynamically determines per-day sending limits. The best way to increase your limit is to send high-quality emails (low bounce rates and complaints). Your limit starts out at 1,000 emails, but it will increase within a few days if you constantly send up to 1,000 high quality emails per day.
配额 – Amazon SES动态确定每日发送限制。 增加限制的最好方法是发送高质量的电子邮件(跳出率和投诉率低)。 您的限制开始于1,000封电子邮件,但是如果您每天不断发送多达1,000封高质量的电子邮件,则限额会在几天内增加。
Limits – Messages cannot be larger than 10MB. If you need to send larger files, I would recommend using Amazon S3 service with secure, time-restricted links rather than attaching large files. Emails are also limited to 50 recipients (combined total of to, cc, and bcc).
限制 –邮件不能超过10MB。 如果您需要发送更大的文件,我建议您将Amazon S3服务与安全的,有时间限制的链接一起使用,而不是附加大文件。 电子邮件也仅限于50个收件人(to,cc和bcc的总和)。
DNS Setup – To maximize delivery rates, Amazon recommends setting up special DNS records that tell other ISPs that your domain authorizes Amazon to send email on your behalf. To learn more, read the “SPF, Sender ID, and DKIM” sections in the Amazon SES documentation.
DNS设置 –为了最大程度地提高传送速度,Amazon建议设置特殊的DNS记录,以告知其他ISP您的域授权Amazon代表您发送电子邮件。 要了解更多信息,请阅读Amazon SES文档中的“ SPF,发件人ID和DKIM”部分。
简直更好 (Simply Better)
Switching my transactional and other system-generated email to Amazon SES has greatly reduced the amount of time, effort, and money that I need to put into maintaining reliable email service in my web applications. And it’s great to know that as my applications grow, I don’t need to worry about outgrowing my email service.
将我的交易和其他系统生成的电子邮件切换到Amazon SES,极大地减少了我在Web应用程序中维护可靠的电子邮件服务所需的时间,精力和金钱。 很高兴知道随着我的应用程序的增长,我不必担心电子邮件服务的增长。
Have questions or suggestions? Post comments below!
有问题或建议吗? 在下面发表评论!
lo云中