如何更改nodemailer中的"from"字段?

How to change the "from" field in nodemailer?

免责声明:我在技术电子邮件方面不是很好。

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

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

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

一旦我停止尝试更改 from 字段,一切都会正常工作(除了 wrong from 字段)。由于我不知道发生了什么,所以我正在寻求一些帮助来解决这个问题。

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

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)

发件人字段必须采用 Display Name <email@address.com>

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

您可以在 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);
});

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

Gmail also always sets authenticated username as the From: email address. So if you authenticate as foo@example.com and set bar@example.com as the from: address, then Gmail reverts this and replaces the sender with the authenticated user.

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

谢谢,如有不妥请指出