Amazon ses.sendEmail - 如何附加 pdf 文件?

问题描述 投票:0回答:3

我正在尝试使用 Amazon ses.sendEmail 在电子邮件中附加 pdf。但我不知道执行此操作的参数键。没有附件,它可以完美工作。这是我尝试过的。

` var ses = new AWS.SES()

            var params = {
                Destination: { 
                    ToAddresses: [
                        'xxx',
                    ]

                },
                Message: { 
                    Body: { 
                        Html: {
                            Data: msg,
                            Charset: 'UTF-8'
                        }

                    },
                    Subject: { /* required */
                        Data: 'Test Mail',
                        Charset: 'UTF-8'
                    }
                },
                Attachment:{

                },
                Source: 'yyy'
            };
            ses.sendEmail(params, function(err, data) {
                if (err) {// an error occurred}
                    oDialog.close();
                    MessageToast.show("Email not sent. Some problem occurred!");
                }
                else {
                    oDialog.close();
                    MessageToast.show("Email sent successfully!");
                }
            });`
sapui5 amazon-ses
3个回答
8
投票

对于想要向 SES 电子邮件添加附件的任何人,以下是我在 NodeJS 中对 lambda 所做的操作:将 Nodemailer 与 SES 传输结合使用。

npm install --save nodemailer

并在代码中:

// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
  SES: new AWS.SES({
    apiVersion: '2010-12-01',
    region: "eu-west-1", // SES is not available in eu-central-1
  })
});

const emailTransportAttachments = [];
if (attachments && attachments.length !== 0) {
  emailTransportAttachments = attachments.map(attachment => ({
    filename: attachment.fileName,
    content: attachment.data,
    contentType: attachment.contentType,
  }));
}
const emailParams = {
  from,
  to,
  bcc,
  subject,
  html,
  attachments: emailTransportAttachments,
};

return new Promise((resolve, reject) => {
  transporter.sendMail(emailParams, (error, info) => {
    if (error) {
      console.error(error);
      return reject(error);
    }
    console.log('transporter.sendMail result', info);
    resolve(info);
  });
});

5
投票

目前,您只能使用原始电子邮件 API 发送附件,例如:

var ses_mail = "From: 'Your friendly UI5 developer' <" + email + ">\n"
    + "To: " + email + "\n"
    + "Subject: AWS SES Attachment Example\n"
    + "MIME-Version: 1.0\n"
    + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
    + "--NextPart\n"
    + "Content-Type: text/html; charset=us-ascii\n\n"
    + "This is the body of the email.\n\n"
    + "--NextPart\n"
    + "Content-Type: text/plain;\n"
    + "Content-Disposition: attachment; filename=\"attachment.txt\"\n\n"
    + "Awesome attachment" + "\n\n"
    + "--NextPart";

var params = {
    RawMessage: { Data: new Buffer(ses_mail) },
    Destinations: [ email ],
    Source: "'Your friendly UI5 developer' <" + email + ">'"
};

var ses = new AWS.SES();

ses.sendRawEmail(params, function(err, data) {
    if(err) {
        oDialog.close();
        MessageToast.show("Email sent successfully!");
    } 
    else {
        oDialog.close();
        MessageToast.show("Email sent successfully!");
    }           
});

0
投票

我修改了 jpenninkhof 对 PDF 文件的答案,以下是如何使用 SES 发送 PDF 附件

let attachment = fs.readFileSync("./uploads/" + filename).toString('base64')

            var ses_mail = "From: <[email protected]" + ">\n"
                + "To: " + email + "\n"
                + "Subject: Your email subject\n"
                + "MIME-Version: 1.0\n"
                + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
                + "--NextPart\n"
                + "Content-Type: text/html; charset=us-ascii\n\n"
                + "This is your email body.\n\n"
                + "--NextPart\n"
                + "Content-Type: application/pdf;\n"
                + "Content-Disposition: attachment; filename= \"filename.pdf\"\n"
                + "Content-Transfer-Encoding: base64\n\n"
                + attachment + "\n\n"
                + "--NextPart";

            var params = {
                RawMessage: { Data: Buffer.from(ses_mail) },
                Destinations: [email],
                Source: "<[email protected]>"
            };

            ses.sendRawEmail(params, function (err, data) {
                if (err) {
                    console.log(err)
                }
                else {
                    console.log("Email sent successfully!");
                }
            });

© www.soinside.com 2019 - 2024. All rights reserved.