How to change the "from" field in nodemailer?

boop picture boop · Nov 27, 2015 · Viewed 19.7k times · Source

Disclaimer: I'm not very good with technical email aspects.

So I've setup up a free zoho mail account which basically is just an mail server for my domain. This kinda works via mx record forwarding or something, I'm not entirely sure how it works.

Anyway, the point is: I can change easily the from field when using my account per Outlook. So my email address [email protected] appears as Foo from bar.com in most email clients.

Now I want to send some automated emails from my [email protected] account with nodemailer (v1.10.0) over SMTP with SSL. I've tried different approaches I've found in the documentation / on the internet. All of them just have thrown an ambigious stack trace (see below).

As soon as I stop trying to change the from field everything works fine (except for the wrong from field). Since I've no idea whats goin on I'm asking for some help troubleshooting this.

I've tried changing the second argument of createTransport() to my desired from field. Did not work.

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

to

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

I've tried setting auth.mail.from which did also not work. And I've tried setting auth.mail.from with passing a 2nd parameter to createTransport().


My code

var nodemailer = require('nodemailer');
var auth = { mail: { host: 'smtp.zoho.com', port: 465, secure: true, auth: { user: '[email protected]', 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);
        }
    });
}

Here the stacktrace I've talked about before

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)

Answer

boop picture boop · Nov 27, 2015

The from field has to be in the the format Display Name <[email protected]>

transporter.sendMail({ ..., from: 'Foo from @bar.com <[email protected]>' });