30

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 foo@bar.com appears as Foo from bar.com in most email clients.

Now I want to send some automated emails from my donotreply@bar.com 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: '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);
        }
    });
}

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)
boop
  • 7,413
  • 13
  • 50
  • 94

3 Answers3

48

The from field has to be in the the format Display Name <email@address.com>

transporter.sendMail({ ..., from: 'Foo from @bar.com <donotreply@bar.com>' });
boop
  • 7,413
  • 13
  • 50
  • 94
16

I had the same issue with the from: field and I went back to read gmail setup and this is what i found - as i said, only GMAIL. I haven't tried a different provider yet.
src: nodemailer.com/usage/using-gmail/
Client: 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.

basically, you can only specify the authenticated user on the from: field

thank you, if I'm wrong, please let me know

Vic B-A
  • 338
  • 1
  • 3
  • 10
9

The first example that you can look on nodemailer.com website.

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);
});
xdeepakv
  • 7,835
  • 2
  • 22
  • 32