aws simple email service (ses) 是一种功能强大、经济高效的解决方案,可以帮助您安全地发送电子邮件,无论是用于交易消息、营销活动还是自动通知。
在这篇博文中,我们将探讨如何使用 aws ses 发送电子邮件,涵盖各种用例,例如发送 html 模板、附件,甚至日历事件。我们将通过实际示例帮助您快速入门。
aws simple email service (ses) 是一项基于云的电子邮件发送服务,旨在帮助数字营销人员和应用程序开发人员发送营销、通知和交易电子邮件。对于各种规模的企业来说,这是一项可靠、可扩展且经济高效的服务。
主要特点:
在我们深入发送电子邮件之前,让我们为您的账户设置 aws ses。
aws ses 要求您验证您计划使用的电子邮件地址或域。
默认情况下,新的 aws 账户位于沙盒环境中,这限制了电子邮件发送功能。
您需要 aws 访问密钥才能以编程方式与 ses 交互。
我们首先使用适用于 node.js 的 aws 开发工具包发送一封简单的纯文本电子邮件。
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 内容的电子邮件,使其更具视觉吸引力。
const params = { source: 'sender@example.com', destination: { toaddresses: ['recipient@example.com'], }, message: { subject: { data: 'welcome to our service!', }, body: { html: { data: ` <html> <body> <h1>welcome!</h1> <p>we're glad to have you on board.</p> </body> </html> `, }, }, }, }; ses.sendemail(params, (err, data) => { if (err) { console.error('error sending html email', err); } else { console.log('html email sent successfully', data); } });
温馨提示:
要发送带有附件的电子邮件,我们将使用 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); } });
说明:
要发送日历事件,我们将包含一个 .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); } });
说明:
aws ses 是一项多功能服务,可以处理各种电子邮件发送需求。无论您是发送简单的通知、包含丰富 html 内容的营销电子邮件,还是包含附件和日历事件的复杂消息,aws ses 都能满足您的需求。
通过遵循本指南,您现在应该充分了解如何:
感谢您的阅读!如果您有任何问题或建议要分享,请随时在下面发表评论。快乐编码!
以上就是使用 AWS SES 发送电子邮件:综合指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号