簡體   English   中英

如何為Amazon SES sendRawEmail創建消息正文

[英]How to create message body for Amazon SES sendRawEmail

我正在嘗試使用AWS Node.js SDK定制標題。

似乎sendEmail API端點隨附的SDK方法不支持自定義標頭。

因此, 合適的方法是sendRawEmail

不幸的是,我找不到很好的信息或實際上可以幫助編寫原始電子郵件的軟件包。 意思是發送純文本和html格式,並根據RFC 2822和RFC 5322創建消息正文和標頭。

組成用JavaScript編寫的原始內容的簡單方法的外觀如何?

您應該看看另一個npm軟件包,該軟件包負責編寫帶有標頭的電子郵件,例如nodemailer 經過幾分鍾的搜索,我在此處找到了一個示例,該示例設置標頭並通過SES發送電子郵件,它們使用的是mailcomposer,它是nodemailer的前身

更新:看來nodemailer不再公開build方法,但是從他們那里還有另一個包nodemailer-ses-transport ,它似乎完全可以滿足您的需求,可以自定義標頭並通過SES發送。

這是我想到的准系統功能,它僅具有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();

}

這些是格式化日期標題的實用方法。 您也可以簡單地使用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 }`;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM