在Meteor应用程序中发送电子邮件时,不会替换Sengrid模板替换标签

时间:2016-07-28 15:50:39

标签: email meteor sendgrid

在包含用于用户邀请和通知的Sendgrid交易电子邮件模板的Meteor应用程序中,我无法替换替换标记。收到模板化的电子邮件,但没有任何区别。

Email.send({
    from: "hello@domain.com",
    to:email,
    subject: "Subject",
    sub: {
      "{name}":post.createdBy,
      "{title}":post.title,
    },
    headers: {
      "X-SMTPAPI": {
        "filters": {
          "templates": {
            "settings": {
              "enable": 1,
              "template_id": "xxxx"
            }
          }
        }
      },
      "Content-Type" : "text/html"
    }
});

我没有直接使用API​​,而是使用Meteor Email包,但是没有看到可能的问题:

Meteor.startup(function () {
  process.env.MAIL_URL = 'smtp://username:password@smtp.sendgrid.net:587';
});

这是我缩短的电子邮件模板:

Hey {name},  

  your post {title} has a new comment.

2 个答案:

答案 0 :(得分:1)

您还需要将sub放在X-SMTPAPI标题中。 X-SMTPAPI标头本身也应包含字符串中的有效JSON。

试试这个:

var xsmtpapi = {
  "filters": {
    "templates": {
      "settings": {
        "enable": 1,
        "template_id": "xxxx"
      }
    }
  },
  "sub": {
    "{name}": post.createdBy,
    "{title}": post.title
  }
}

Email.send({
    from: "hello@domain.com",
    to:email,
    subject: "Subject",
    sub: {
      "{name}":post.createdBy,
      "{title}":post.title,
    },
    headers: {
      "X-SMTPAPI": JSON.stringify(xsmtpapi),
      "Content-Type" : "text/html"
    }
});

答案 1 :(得分:0)

我最终做的是使用smtpapi-nodejs NPM包。

简单的例子是:

var nodemailer = require('nodemailer');
var smtpapi    = require('smtpapi');

var header = new smtpapi();

header.setFilters({
    "templates": {
      "settings": {
        "enable": 1,
        "template_id": xxx-template-id-xxx
      }
    }
});

header.addSubstitution('-name-', post.createdBy);
header.addSubstitution(-title-', post.title);

var headers = { 'x-smtpapi': header.jsonString() };

// Use nodemailer to send the email 
var settings  = {
  host: "smtp.sendgrid.net",
  port: parseInt(587, 10),
  requiresAuth: true,
  auth: {
    user: "sendgrid_username",
    pass: "sendgrid_password"
  }
};
var smtpTransport = nodemailer.createTransport(settings);

var mailOptions = {
  from:     "Fred Foo <foo@blurdybloop.com>",
  to:       "bar@blurdybloop.com",
  subject:  "Hello",
  text:     "Hello world",
  html:     "<b>Hello world</b>",
  headers:  headers
}

smtpTransport.sendMail(mailOptions, function(error, response) {
  smtpTransport.close();

  if (error) {
    console.log(error);
  } else {
    console.log("Message sent: " + response.message);
  } 
});