【问题标题】:How to change the "from" field in nodemailer?如何更改 nodemailer 中的“发件人”字段?
【发布时间】:2015-11-27 03:49:42
【问题描述】:

免责声明:我不太擅长电子邮件技术方面。

所以我设置了一个免费的zoho mail 帐户,它基本上只是我的域的邮件服务器。这有点通过 mx 记录转发或其他方式工作,我不完全确定它是如何工作的。

无论如何,重点是:在每个 Outlook 中使用我的帐户时,我可以轻松更改 from 字段。所以我的电子邮件地址foo@bar.com 在大多数电子邮件客户端中显示为Foo from bar.com

现在我想通过带有 SSL 的 SMTP 从我的 donotreply@bar.com 帐户使用 nodemailer (v1.10.0) 发送一些自动电子邮件。我尝试了在文档/互联网上找到的不同方法。他们都只是抛出了一个模棱两可的堆栈跟踪(见下文)。

一旦我停止尝试更改 from 字段,一切正常(wrong from 字段除外)。由于我不知道发生了什么,所以我请求帮助解决此问题。

我尝试将createTransport() 的第二个参数更改为我想要的字段。没用。

nodemailer.createTransport(auth.mail, {from: auth.mail.auth.user});

nodemailer.createTransport(auth.mail, {from: 'Foo from bar.com'});

我试过设置auth.mail.from,但也没有用。我已经尝试通过将第二个参数传递给createTransport() 来设置auth.mail.from


我的代码

var nodemailer = require('nodemailer');
var auth = { mail: { host: 'smtp.zoho.com', port: 465, secure: true, auth: { user: 'donotreply@bar.com', pass: 'strongpassword' } };
var log = require('./log');

var transporter = nodemailer.createTransport(auth.mail, {from: auth.mail.auth.user});

function sendText(settings,cb) {
    transporter.sendMail(settings, function (err, info) {
        if (err) {
            log.warn('Failed to send an Email', err);
        } else {
            log.info('Successfully sent email', info);
        }
        if (cb) {
            cb(err, info);
        }
    });
}

这里是我之前谈到的堆栈跟踪

Message failed
    at SMTPConnection._formatError (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:388:15)
    at SMTPConnection._actionStream (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:948:30)
    at SMTPConnection.<anonymous> (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:579:14)
    at SMTPConnection._processResponse (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:511:16)
    at SMTPConnection._onData (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:357:10)
    at emitOne (events.js:77:13)
    at TLSSocket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at TLSSocket.Readable.push (_stream_readable.js:110:10)
    at TLSWrap.onread (net.js:523:20)

【问题讨论】:

    标签: node.js nodemailer


    【解决方案1】:

    发件人字段的格式必须为Display Name &lt;email@address.com&gt;

    transporter.sendMail({ ..., from: 'Foo from @bar.com <donotreply@bar.com>' });
    

    【讨论】:

      【解决方案2】:

      我在from: 字段中遇到了同样的问题,我回去阅读 gmail 设置,这就是我发现的 - 正如我所说,只有 GMAIL。我还没有尝试过其他提供商。
      源代码:nodemailer.com/usage/using-gmail/
      客户端:Gmail

      Gmail 还始终将经过身份验证的用户名设置为 From: 电子邮件 地址。因此,如果您以 foo@example.com 身份进行身份验证并设置 bar@example.com 作为 from: 地址,然后 Gmail 将其还原并 用经过身份验证的用户替换发件人。

      基本上,您只能在from: 字段上指定经过身份验证的用户

      谢谢,如果我错了,请告诉我

      【讨论】:

      • 有没有办法覆盖这个?
      • 没有办法@JoelHarkes
      • 今天也验证了这个^
      【解决方案3】:

      您可以在nodemailer.com 网站上查看的第一个示例。

      var nodemailer = require('nodemailer');
      
      // create reusable transporter object using the default SMTP transport
      var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');
      /*set from user in option*/
      var mailOptions = {
          from: 'Fred Foo ? <foo@blurdybloop.com>', // sender address
          to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
          subject: 'Hello ✔', // Subject line
          text: 'Hello world ?', // plaintext body
          html: '<b>Hello world ?</b>' // html body
      };
      
      transporter.sendMail(mailOptions, function(error, info){
          if(error){
              return console.log(error);
          }
          console.log('Message sent: ' + info.response);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-20
        • 1970-01-01
        • 2016-09-24
        • 2011-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多