
aws simple email service (ses) 是一种功能强大、经济高效的解决方案,可以帮助您安全地发送电子邮件,无论是用于交易消息、营销活动还是自动通知。
在这篇博文中,我们将探讨如何使用 aws ses 发送电子邮件,涵盖各种用例,例如发送 html 模板、附件,甚至日历事件。我们将通过实际示例帮助您快速入门。
目录
- 什么是 aws ses?
- 设置 aws ses
- 发送简单的电子邮件
- 发送 html 电子邮件
- 发送带附件的电子邮件
- 发送日历邀请
- 最佳实践
- 结论
什么是 aws ses?
aws simple email service (ses) 是一项基于云的电子邮件发送服务,旨在帮助数字营销人员和应用程序开发人员发送营销、通知和交易电子邮件。对于各种规模的企业来说,这是一项可靠、可扩展且经济高效的服务。
主要特点:
- 可扩展性:轻松处理大量电子邮件。
- 交付率: 由于 aws 的声誉,交付率很高。
- 成本效益:即用即付定价模式。
- 安全性:支持dkim和spf等身份验证机制。
设置 aws ses
在我们深入发送电子邮件之前,让我们为您的账户设置 aws ses。
第 1 步:验证您的电子邮件地址或域名
aws ses 要求您验证您计划使用的电子邮件地址或域。
- 验证电子邮件地址:
- 转到 aws ses 控制台。
- 导航到身份管理下的电子邮件地址。
- 点击验证新电子邮件地址。
- 输入您的电子邮件地址,然后单击验证此电子邮件地址。
- 检查您的收件箱并单击来自 aws 的电子邮件中的验证链接。
- 验证域名:
- 导航到身份管理下的域。
- 点击验证新域名。
- 输入您的域名。
- aws 将提供 dns 记录。将这些添加到您域的 dns 设置中。
第 2 步:请求生产访问权限
默认情况下,新的 aws 账户位于沙盒环境中,这限制了电子邮件发送功能。
- 前往ses发送限制页面。
- 点击请求提高发送限额。
- 填写退出沙箱的申请表。
步骤 3:设置 aws 凭证
您需要 aws 访问密钥才能以编程方式与 ses 交互。
- 转到 aws iam 控制台。
- 使用编程访问创建新用户。
- 附加 amazonsesfullaccess 策略。
- 保存您的访问密钥id和秘密访问密钥。
发送简单的电子邮件
我们首先使用适用于 node.js 的 aws 开发工具包发送一封简单的纯文本电子邮件。
先决条件
- node.js 安装在您的计算机上。
- 已安装适用于 node.js 的 aws 开发工具包 (aws-sdk)。
代码示例
const aws = require('aws-sdk');
// configure aws sdk
aws.config.update({
accesskeyid: 'your_access_key_id',
secretaccesskey: 'your_secret_access_key',
region: 'us-east-1', // replace with your ses region
});
const ses = new aws.ses();
const params = {
source: 'sender@example.com',
destination: {
toaddresses: ['recipient@example.com'],
},
message: {
subject: {
data: 'test email from aws ses',
},
body: {
text: {
data: 'hello, this is a test email sent using aws ses!',
},
},
},
};
ses.sendemail(params, (err, data) => {
if (err) {
console.error('error sending email', err);
} else {
console.log('email sent successfully', data);
}
});
说明:
- 来源:您发送邮件时所使用的经过验证的电子邮件地址。
- 目的地:收件人的电子邮件地址。
- 消息:包含电子邮件的主题和正文。
发送 html 电子邮件
现在,让我们发送一封包含 html 内容的电子邮件,使其更具视觉吸引力。
基于Intranet/Internet 的Web下的办公自动化系统,采用了当今最先进的PHP技术,是综合大量用户的需求,经过充分的用户论证的基础上开发出来的,独特的即时信息、短信、电子邮件系统、完善的工作流、数据库安全备份等功能使得信息在企业内部传递效率极大提高,信息传递过程中耗费降到最低。办公人员得以从繁杂的日常办公事务处理中解放出来,参与更多的富于思考性和创造性的工作。系统力求突出体系结构简明
代码示例
const params = {
source: 'sender@example.com',
destination: {
toaddresses: ['recipient@example.com'],
},
message: {
subject: {
data: 'welcome to our service!',
},
body: {
html: {
data: `
welcome!
we're glad to have you on board.
`,
},
},
},
};
ses.sendemail(params, (err, data) => {
if (err) {
console.error('error sending html email', err);
} else {
console.log('html email sent successfully', data);
}
});
温馨提示:
- 您可以内联包含 css 样式或使用基本样式来确保跨电子邮件客户端的兼容性。
- 始终包含纯文本版本作为后备。
发送带附件的电子邮件
要发送带有附件的电子邮件,我们将使用 sendrawemail 方法而不是 sendemail。
代码示例
const fs = require('fs');
const path = require('path');
const aws = require('aws-sdk');
const ses = new aws.ses();
// read the attachment file
const filepath = path.join(__dirname, 'attachment.pdf');
const filecontent = fs.readfilesync(filepath);
// define the email parameters
const params = {
rawmessage: {
data: createrawemail(),
},
};
function createrawemail() {
const boundary = '----=_part_0_123456789.123456789';
let rawemail = [
`from: sender@example.com`,
`to: recipient@example.com`,
`subject: email with attachment`,
`mime-version: 1.0`,
`content-type: multipart/mixed; boundary="${boundary}"`,
``,
`--${boundary}`,
`content-type: text/plain; charset=utf-8`,
`content-transfer-encoding: 7bit`,
``,
`hello, please find the attached document.`,
`--${boundary}`,
`content-type: application/pdf; name="attachment.pdf"`,
`content-description: attachment.pdf`,
`content-disposition: attachment; filename="attachment.pdf";`,
`content-transfer-encoding: base64`,
``,
filecontent.tostring('base64'),
`--${boundary}--`,
].join('\n');
return rawemail;
}
ses.sendrawemail(params, (err, data) => {
if (err) {
console.error('error sending email with attachment', err);
} else {
console.log('email with attachment sent successfully', data);
}
});
说明:
- 多部分 mime 消息: 我们构建带有 mime 边界的原始电子邮件以包含附件。
- base64 编码: 附件必须采用 base64 编码。
- 内容标头:电子邮件客户端需要正确的标头才能正确解释附件。
发送日历邀请
要发送日历事件,我们将包含一个 .ics 文件作为附件。
代码示例
function createCalendarEvent() {
const event = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'DTSTAMP:20231016T090000Z',
'DTSTART:20231020T100000Z',
'DTEND:20231020T110000Z',
'SUMMARY:Meeting Invitation',
'DESCRIPTION:Discuss project updates',
'LOCATION:Conference Room',
'END:VEVENT',
'END:VCALENDAR',
].join('\n');
return Buffer.from(event).toString('base64');
}
function createRawEmail() {
const boundary = '----=_Part_0_123456789.123456789';
let rawEmail = [
`From: sender@example.com`,
`To: recipient@example.com`,
`Subject: Meeting Invitation`,
`MIME-Version: 1.0`,
`Content-Type: multipart/mixed; boundary="${boundary}"`,
``,
`--${boundary}`,
`Content-Type: text/plain; charset=UTF-8`,
`Content-Transfer-Encoding: 7bit`,
``,
`Hello, you're invited to a meeting.`,
`--${boundary}`,
`Content-Type: text/calendar; method=REQUEST; name="invite.ics"`,
`Content-Transfer-Encoding: base64`,
`Content-Disposition: attachment; filename="invite.ics"`,
``,
createCalendarEvent(),
`--${boundary}--`,
].join('\n');
return rawEmail;
}
ses.sendRawEmail(params, (err, data) => {
if (err) {
console.error('Error sending calendar invite', err);
} else {
console.log('Calendar invite sent successfully', data);
}
});
说明:
- 日历事件格式:我们使用 icalendar 格式创建一个 .ics 文件内容。
- method=request: 表示这是会议邀请。
- 正确的标题:确保日历文件的内容类型和内容处置正确。
最佳实践
- 错误处理:始终在您的应用程序中包含强大的错误处理。
- 电子邮件验证:发送前确保电子邮件地址有效。
- 限制: 请注意您的 ses 发送限制以避免限制。
- 取消订阅链接:在营销电子邮件中包含取消订阅选项以遵守法规。
- 监控:使用 aws cloudwatch 监控您的电子邮件发送活动。
- 安全性:保护您的 aws 凭证并尽可能使用 iam 角色。
结论
aws ses 是一项多功能服务,可以处理各种电子邮件发送需求。无论您是发送简单的通知、包含丰富 html 内容的营销电子邮件,还是包含附件和日历事件的复杂消息,aws ses 都能满足您的需求。
通过遵循本指南,您现在应该充分了解如何:
- 为您的账户设置 aws ses。
- 发送纯文本和 html 电子邮件。
- 在电子邮件中包含附件和日历邀请。
感谢您的阅读!如果您有任何问题或建议要分享,请随时在下面发表评论。快乐编码!









