如何在`Node aws-sdk` sendRawEmail 函数中发送 PDF 附件? [英] How can send PDF attachment in `Node aws-sdk` sendRawEmail function?

查看:20
本文介绍了如何在`Node aws-sdk` sendRawEmail 函数中发送 PDF 附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sendRawEmail(Node: aws-sdk) 函数以附件形式发送 PDF 文件,我尝试了很多方法,电子邮件发送成功,但 PDF 变得简单.请更正我的代码并帮助解决它.

I want to send PDF file in attachment using sendRawEmail(Node: aws-sdk) function, I have tried lots of ways, email sends successfully but PDF goes plain. Please correct my code and help to solve it.

代码在这里:

try {
    data = fs.readFileSync('files/demo-invoice-new.pdf', 'utf8');

    console.log(data.toString());

    var ses_mail = "From: 'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">
";
    ses_mail = ses_mail + "To: " + toEmail + "
";
    ses_mail = ses_mail + "Subject: AWS SES Attachment Example
";
    ses_mail = ses_mail + "MIME-Version: 1.0
";
    ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary="NextPart"

";
    ses_mail = ses_mail + "--NextPart
";
    ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii

";
    ses_mail = ses_mail + "This is the body of the email.

";
    ses_mail = ses_mail + "--NextPart
";
    ses_mail = ses_mail + "Content-Type: application/octet;
";
    ses_mail = ses_mail + "Content-Disposition: attachment; filename="demo-invoice-new.pdf"

";
    ses_mail = ses_mail + data.toString('utf8') + "

";
    ses_mail = ses_mail + "--NextPart--";

    var params = {
        RawMessage: { Data: new Buffer(ses_mail) },
        Destinations: [toEmail],
        Source: "'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">'"
    };

    console.log(params);

    var sendPromise = new AWS.SES(AWS_SES_CONFIG).sendRawEmail(params).promise();

    return sendPromise.then(
        data => {
            console.log(data);
            return data;
        }).catch(
        err => {
            console.error(err.message);
            throw err;
        });
} catch (e) {
    console.log('Error:', e.stack);
}

推荐答案

SES 原始消息 必须是 base64 编码的.因此,您需要将文件内容作为缓冲区并将其编码为 base64 字符串附件.此外,您无需为原始消息数据创建新缓冲区,因为它已经接受字符串数据类型.

SES raw messages must be base64-encoded. So, you'll need to get the file content as buffer and encode it as base64 string attachment. Also, you don't need to create a new buffer for raw message data since it already accepts a string data type.

可选:您也可以省略 Destinations 参数,因为您已经在原始消息数据中提供了 To 字段.(您也可以提供 CcBcc 字段)

OPTIONAL: You can also omit the Destinations parameter since you're already providing the To field in the raw message data. (You can also provide the Cc and Bcc fields as well)

你可以试试这个,例如:

You could try this for example:

data = fs.readFileSync("files/demo-invoice-new.pdf");

var ses_mail = "From: 'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">
";
ses_mail += "To: " + toEmail + "
";
ses_mail += "Subject: AWS SES Attachment Example
";
ses_mail += "MIME-Version: 1.0
";
ses_mail += "Content-Type: multipart/mixed; boundary="NextPart"

";
ses_mail += "--NextPart
";
ses_mail += "Content-Type: text/html

";
ses_mail += "This is the body of the email.

";
ses_mail += "--NextPart
";
ses_mail += "Content-Type: application/octet-stream; name="demo-invoice-new.pdf"
";
ses_mail += "Content-Transfer-Encoding: base64
";
ses_mail += "Content-Disposition: attachment

";
ses_mail += data.toString("base64").replace(/([^]{76})/g, "$1
") + "

";
ses_mail += "--NextPart--";

var params = {
    RawMessage: {Data: ses_mail},
    Source: "'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">'"
};

注意:/([^]{76})/ 正则表达式替换会打断您的长行以确保邮件服务器不会抱怨消息当存在编码附件时,行太长,这可能会导致瞬时反弹.(请参阅 RFC 5321)

NOTE: The /([^]{76})/ regular expression replacement breaks your long lines to make sure that mail servers do not complain about message lines being too long when there is an encoded attachment, which might result in a transient bounce. (See RFC 5321)

这篇关于如何在`Node aws-sdk` sendRawEmail 函数中发送 PDF 附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆