Sno*_*all 3 javascript amazon-web-services node.js aws-sdk amazon-simple-email-service
我尝试在使用 AWS Node.js SDK 时自定义标头。
sendEmail API 端点和随附的 SDK 方法似乎不支持自定义标头。
因此,合适的方法是sendRawEmail。
不幸的是,我找不到真正有助于撰写原始电子邮件的良好信息或软件包。这意味着发送纯文本和 html 格式并根据 RFC 2822 和 RFC 5322 创建消息正文和标头。
用 JavaScript 编写的组成原始主体的简单方法会是什么样子?
这是我想出的一个准系统函数,它只有作为AWS Javascript SDK依赖项。我一开始做错的一件事是 Base 64 编码RawMessage.Data,但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();
}
Run Code Online (Sandbox Code Playgroud)
这些是格式化日期标题的实用方法。您也可以简单地使用该moment库moment().format('ddd, DD MMM YYYY HH:MM:SS ZZ');,但这并不重要,因为日期标头无论如何都会被 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 }`;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13277 次 |
| 最近记录: |