NodeMailer附件没有使用smtp/gmail发送

soc*_*ngh 5 node.js nodemailer

我使用NodeMailer创建了以下功能,它似乎在没有问题的情况下发送电子邮件(收到控制台和电子邮件中的"消息发送"通知),除了没有发送任何电子邮件的附件!

尝试了一堆电子邮件地址(gmail,谷歌应用程序,hotmail),但所有人都在做同样的事情.请帮忙!

var sendWithAttachment = function(userMail, subject, html, attachment_path, cb){
  var smtpTransport = nodemailer.createTransport("SMTP",{
      service: "Gmail",
      auth: {
          user: "labs@domain.com",
          pass: "password"
      }
  });

  var mailOptions = {
      from: "Labs <labs@domain.com>",
      to: userMail,
      subject: subject || "[blank]"
      html: html || "[none]"
      generateTextFromHTML: true,
      attachments: [
          {   // filename and content type is derived from path
              path: attachment_path
          },
          {   // utf-8 string as an attachment
              filename: 'check.txt',
              content: 'checking that some attachments work...'
          },
      ],
  };

  smtpTransport.sendMail(mailOptions, function(error, response){
      if(error){
          console.log(error);
          cb(error, null);
      }else{
          console.log("Message sent: " + response.message);
          cb(null, response);
      }
      smtpTransport.close();
  });
};
Run Code Online (Sandbox Code Playgroud)

小智 10

这是nodemailer文档中的一个问题.使用'filePath'更改'path'以定义路径并将'content'更改为文本的'contents'.为我工作.

var mailOptions = {
    ...
    attachments: [
        {   // utf-8 string as an attachment
            filename: 'text1.txt',
            contents: 'hello world!'
        },
        {   // utf-8 string as an attachment
            filename: 'text1.txt',
            filePath: 'text.txt'
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)


Cli*_*ell 4

content我通过重命名为 解决了这个问题contents。我正在阅读最新版本的nodemailer. 您可以在此处阅读低于1.0版本的文档: https: //github.com/andris9/Nodemailer/blob/0.7/README.md