简体   繁体   English

使用 nodemailer 发送带有附件和 html 数据的邮件

[英]Sending mail with attachments and html data using nodemailer

The problem I ran into is that I can only send an email with attachments OR template data, but I haven't found a way to send both.我遇到的问题是我只能发送带有附件或模板数据的电子邮件,但我还没有找到同时发送两者的方法。

Here's my code:这是我的代码:

var EmailTemplate = require('email-templates').EmailTemplate;
var template = new EmailTemplate('templates/welcome');
template.render({}, function(err, results) {
  var send = transporter.templateSender({
    from: '<ouremail@gmail.com>',
    html: results.html,
    attachments: [{
      filename: 'file1.png',
      path: 'templates/file1.png',
      cid: 'file1'
    }, {
      filename: 'file2.png',
      path: 'templates/file2.png',
      cid: 'file2'
    }]
  });

  send({
    to: String(user.emailAddress),
    subject: 'Welcome to the Our Site!'
  }, {name: user.firstName}, callback);
});

If I do it the way shown above, the name template variable renders, but the pictures don't show up.如果我按照上面显示的方式进行操作,名称模板变量会呈现,但图片不会显示。 If I instead do如果我改为

var send = transporter.templateSender(template, ...

then the attachments render, and not the template variable.然后附件呈现,而不是模板变量。

I know that I could use a templating package like Jade or Handlebars for this kind of thing but that seems like overkill when nodemailer has all of the functionality I need.我知道我可以使用像 Jade 或 Handlebars 这样的模板包来做这种事情,但是当 nodemailer 拥有我需要的所有功能时,这似乎有点过分了。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

I was doing exactly same and I wasted my precious 4 hours to solve this just before Go LIVE.. But finally I found answer to this.我做的完全一样,在上线之前我浪费了宝贵的 4 个小时来解决这个问题..但最后我找到了答案。

The place to keep attachments is in the send() part rather than templateSender() , so the code should look like this:保存附件的位置在send()部分而不是templateSender() ,因此代码应如下所示:

send({
    to: String(user.emailAddress),
    subject: 'Welcome to the Our Site!',
    attachments:[{
      filename: 'file1.png',
      path: 'templates/file1.png',
      cid: 'file1'
    }, {
      filename: 'file2.png',
      path: 'templates/file2.png',
      cid: 'file2'
    }]
  }, {name: user.firstName}, callback);

Hope it helps someone!希望它可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM