在AWS Lambda上运行时发送带有附件的电子邮件(sendRawEmail)不发送电子邮件

时间:2019-04-04 16:20:44

标签: amazon-web-services aws-lambda email-attachments amazon-ses

我有以下代码,当我在本地运行代码时,它可以很好地发送电子邮件,但是在将相同的代码放在AWS lambda(没有VPC并具有SES完全访问属性角色)上时,它会很好地工作,发送电子邮件,我在做什么错了?

const aws = require('aws-sdk');
aws.config.update({
  "region": "eu-west-1",
  accessKeyId: 'myAccessKey',
  secretAccessKey: 'mySecretAccessKey'
});

/**
 * Send email with attachment.
 * 
 * @param {string} sourceEmail
 * @param {string} toEmail
 * @param {string} subject
 * @param {string} body
 * @param {string} fileName
 * @param {buffer} attachmentBuffer
 * 
 * @returns {void}
 */
module.exports.sendEmailWithAttachment = async (sourceEmail, toEmail, subject, body, fileName, attachmentBuffer) => {

    try {

        var sesEmail = "From: '" + subject + "' <" + sourceEmail + ">\n";
        sesEmail += "To: " + toEmail + "\n";
        sesEmail += "Subject: " + subject + "\n";
        sesEmail += "MIME-Version: 1.0\n";
        sesEmail += "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
        sesEmail += "--NextPart\n";
        sesEmail += "Content-Type: text/html\n\n";
        sesEmail += body+"\n\n";
        sesEmail += "--NextPart\n";
        sesEmail += "Content-Type: application/octet-stream; name=\"" + fileName + "\"\n";
        sesEmail += "Content-Transfer-Encoding: base64\n";
        sesEmail += "Content-Disposition: attachment\n\n";
        sesEmail += attachmentBuffer.toString("base64").replace(/([^\0]{76})/g, "$1\n") + "\n\n";
        sesEmail += "--NextPart--";

        var params = {
            RawMessage: {Data: sesEmail},
            Source: "'"+ subject + "' <" + sourceEmail + ">'"
        };


        await new aws.SES().sendRawEmail(params).promise();        


    } catch (err){

        console.log(err);
    }
};

源电子邮件也已列入白名单,并且也在AWS SES仪表板上进行了验证。

还要注意,我在cloudwatch日志中没有看到任何错误。

有人对我想念的东西有任何想法吗?

在本地运行没有任何问题!附件也很好。

谢谢。

0 个答案:

没有答案