作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在使用 AWS Node.js SDK 时自定义 header 。
似乎sendEmail API endpoint和 accompanying SDK method不支持自定义 Headers。
所以使用的适当方法是 sendRawEmail .
不幸的是,我找不到真正有助于撰写原始电子邮件的好信息或软件包。意味着发送纯文本和 html 格式,并根据 RFC 2822 和 RFC 5322 创建消息正文和 header 。
组成用 JavaScript 编写的原始主体的简单方法会是什么样子?
最佳答案
这是我想出的一个准系统功能,它只有 AWS Javascript SDK
作为依赖项。一开始我做错的一件事是对 RawMessage.Data
进行 Base 64 编码,但 AWS SDK
已经解决了这个问题。
空行 \n
也很重要。
const sendRawEmail = async () => {
// Set up from_name, from_email, to, subject, message_id, plain_text, html and configuration_set variables from database or manually
var date = new Date();
var boundary = `----=_Part${ Math.random().toString().substr( 2 ) }`;
var rawMessage = [
`From: "${ from_name }" <${ from_email }>`, // Can be just the email as well without <>
`To: ${ to }`,
`Subject: ${ subject }`,
`MIME-Version: 1.0`,
`Message-ID: <${ message_id }@eu-west-1.amazonses.com>`, // Will be replaced by SES
`Date: ${ formatDate( date ) }`, // Will be replaced by SES
`Return-Path: <${ message_id }@eu-west-1.amazonses.com>`, // Will be replaced by SES
`Content-Type: multipart/alternative; boundary="${ boundary }"`, // For sending both plaintext & html content
// ... you can add more headers here as decribed in https://docs.aws.amazon.com/ses/latest/DeveloperGuide/header-fields.html
`\n`,
`--${ boundary }`,
`Content-Type: text/plain; charset=UTF-8`,
`Content-Transfer-Encoding: 7bit`,
`\n`,
plain_text,
`--${ boundary }`,
`Content-Type: text/html; charset=UTF-8`,
`Content-Transfer-Encoding: 7bit`,
`\n`,
html,
`\n`,
`--${ boundary }--`
]
// Send the actual email
await ses.sendRawEmail( {
Destinations: [
to
],
RawMessage: {
Data: rawMessage.join( '\n' )
},
Source: from_email, // Must be verified within AWS SES
ConfigurationSetName: configuration_set, // optional AWS SES configuration set for open & click tracking
Tags: [
// ... optional email tags
]
} ).promise();
}
这些是格式化日期标题的实用方法。您也可以简单地使用 moment
库,例如 moment().format('ddd, DD MMM YYYY HH:MM:SS ZZ');
但它不会'这真的很重要,因为日期 header 无论如何都会被 AWS SES 覆盖。
// Outputs timezone offset in format ZZ
const getOffset = ( date ) => {
var offset = - date.getTimezoneOffset();
var offsetHours = Math.abs( Math.floor( offset / 60 ) );
var offsetMinutes = Math.abs( offset ) - offsetHours * 60;
var offsetSign = ( offset > 0 ) ? '+' : '-';
return offsetSign + ( '0' + offsetHours ).slice( -2 ) + ( '0' + offsetMinutes ).slice( -2 );
}
// Outputs two digit inputs with leading zero
const leadingZero = ( input ) => ( '0' + input ).slice( -2 );
// Formats date in ddd, DD MMM YYYY HH:MM:SS ZZ
const formatDate = ( date ) => {
var weekdays = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ];
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
var weekday = weekdays[ date.getDay() ];
var day = leadingZero( date.getDate() );
var month = months[ date.getMonth() ];
var year = date.getFullYear();
var hour = leadingZero( date.getHours() );
var minute = leadingZero( date.getMinutes() );
var second = leadingZero( date.getSeconds() );
var offset = getOffset( date );
return `${ weekday }, ${ day } ${ month } ${ year } ${ hour }:${ minute }:${ second } ${ offset }`;
}
关于javascript - 如何为 Amazon SES sendRawEmail 创建邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54212231/
我是一名优秀的程序员,十分优秀!