Nodemailer transporter - 如何访问标准邮件字段?

时间:2016-12-07 06:58:02

标签: javascript node.js email-integration nodemailer

我正在编写一个简单的nodemailer传输器,但我无法找到记录实际邮件消息数据结构的位置。

我的send方法看起来像这样

send: function(mail, callback) {
  var data = {
    messageId: mail.message.getHeader('message-id'),
    from: mail.message.from,
    to: mail.message.to
  }
  console.log('data', data)
  // do stuff with data
  // etc
};

当我尝试使用以下消息时:

var MYTestTransport = require('./');
var email = {
  headers: {
    'message-id': 'a very nice test'
  },
  from: "Bob <bob@test.tes>",
  to: "Alice <alice@test.tes>",
  subject: "Great Idea",
  text: 'This is a test.',
  html: '<p>This is a <strong>test</strong></p>'
};
var transport = nodemailer.createTransport(MYTestTransport());

transport.sendMail(email, function(err, data) {
  expect(err).to.not.exist;
  expect(data.messageId).to.equal('a very nice test');
  expect(data.from).to.equal('Bob <bob@test.tes>');
  expect(data.to).to.equal('Alice <alice@test.tes>')
  done()
});

然而data最终成为

{
  messageId: 'a very nice test',
  to: undefined,
  from: undefined,
  subject: undefined
}

如何正确阅读邮件字段?这是在任何地方记录的吗?

1 个答案:

答案 0 :(得分:0)

根据git example,将fromto写为:

var email = {
   headers: {
  'message-id': 'a very nice test'
},
from: '"Bob" <bob@test.tes>',
to: '"Alice" <alice@test.tes>',
subject: "Great Idea",
text: 'This is a test.',
html: '<p>This is a <strong>test</strong></p>')
};