【问题标题】:How to send mail with attachment in lambda nodeJs using SES service如何使用 SES 服务在 lambda nodeJs 中发送带有附件的邮件
【发布时间】:2020-04-09 12:00:34
【问题描述】:

我正在尝试发送带有附件的邮件。 我已经使用 SES 服务发送简单和 HTML 内容的邮件。但现在我想发送带有附件的邮件。

我正在使用亚马逊 SES 服务发送电子邮件,并且我正在使用“sendRawEmail”方法发送带有附件的邮件。

我收到这样的错误消息。InvalidParameterValue: Nested group

我没有找到此类错误的任何节点示例。

【问题讨论】:

    标签: email aws-lambda attachment amazon-ses


    【解决方案1】:

    我在尝试使用 NodeJS SES API 发送电子邮件时非常沮丧。 我发现了这个问题,并使用mailcomposer npm 包解决了这个问题。 现在我可以发送带有附件的邮件了。

    安装 AWS 开发工具包

    npm i aws-sdk
    

    现在安装 manilcomposer

    npm i mailcomposer
    

    完整代码如下

    请求正文: 如果你想发送带有 Base64 内容的邮件。

    {
      email: ['jen****@gmail.com', 'Aa****@gmail.com'],
      from: 'no-reply@*****.com',
      subject: 'Sending emails with attachments',
      text: 'please find attachments',
      attachments: [{
        filename: 'sample.pdf',
        content: {{file}}, // file content base64 staring
        encoding: 'base64',
      }]
    }
    

    如果你想发送带有文件路径的邮件。

    {
          email: ['jen****@gmail.com', 'Aa****@gmail.com'],
          from: 'no-reply@*****.com',
          subject: 'Sending emails with attachments',
          text: 'please find attachments',
          attachments: [{
            filename: 'sample.pdf',
            path: './home/sample,pdf'
          }]
        }
    

    业务逻辑代码:

    const ses = new AWS.SES({ region: 'us-east-1' }); //specify region for the particular see service.
    
    const mailcomposer = require('mailcomposer');
    
    exports.handler = function (event, context) {
      console.log('Event: ',JSON.stringify(event));
    
      if (!event.email) {
        context.fail('Missing argument: email');
        return;
      }
    
      if (!event.subject) {
        context.fail('Missing argument: subject');
      }
    
      if (!event.from) {
        context.fail('Missing argument: from');
      }
    
      if (!event.html && !event.text) {
        context.fail('Missing argument: html|text');
      }
    
      const to = event.email;
      const from = event.from;
      const subject = event.subject;
      const htmlBody = event.html; 
      const textBody = event.text;
      const attachments = event.attachments;
    
      const mailOptions = {
        from: from,
        subject: subject,
        text: textBody,
        html: htmlBody,
        to: to,
        attachments: attachments ? attachments : []
      };
    
      const mail = mailcomposer(mailOptions);
    
      mail.build(function (err, message) {
        const req = ses.sendRawEmail({ RawMessage: { Data: message } });
    
        req.on('build', function () {
          req.httpRequest.headers['Force-headers'] = '1';
        });
    
        req.send(function (err, data) {
          if (err) {
            console.log(err, err.stack);
            context.fail('Internal Error: The email could not be sent.');
          } else {
            console.log(message);
            console.log('The email was successfully sent')
            context.succeed('The email was successfully sent');
          }
        });
      });
    };
    
    if you have and any query regarding mailcomposer then check [here][1] this link below
    
      [1]: https://nodemailer.com/extras/mailcomposer/
    

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2019-03-05
      • 2018-09-27
      • 2014-12-31
      • 2020-08-23
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多