亚马逊ses.sendEmail - 如何附加pdf文件?

时间:2016-09-22 15:12:14

标签: sapui5 amazon-ses

我正在尝试使用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!");
                }
            });`

2 个答案:

答案 0 :(得分:1)

目前,如果您使用原始电子邮件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!");
    }           
});

答案 1 :(得分:1)

对于其他想要为SES电子邮件添加附件的人,以下是我在nodejs中为lambda做的事情:使用带有SES传输的nodemailer。

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);
  });
});