Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the "from" field in nodemailer?

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)
like image 898
boop Avatar asked Nov 27 '15 03:11

boop


People also ask

What is transporter in Nodemailer?

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.

How do I create a custom domain email in Nodemailer?

You need to configure a transporter with your custom domain info (host, port, user and password) You can find this info in the email configuration of your specific hosting provider. Hi there! I get the message 'email sent' and the function finishes with the status 'ok'.

Does Nodemailer go to spam?

There are numerous reasons why this can happen, and here are some of them: Your host is marked as a spam - this happens if you have not verified your e-mail or you are sending too much e-mails from the same host. Shared hosting is commonly marked as such, and therefore mail server will regularly mark them as a spam.


3 Answers

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

transporter.sendMail({ ..., from: 'Foo from @bar.com <[email protected]>' });
like image 67
boop Avatar answered Oct 07 '22 00:10

boop


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 [email protected] and set [email protected] 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

like image 22
Vic B-A Avatar answered Oct 07 '22 01:10

Vic B-A


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:[email protected]');
/*set from user in option*/
var mailOptions = {
    from: 'Fred Foo 👥 <[email protected]>', // sender address
    to: '[email protected], [email protected]', // 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);
});
like image 22
xdeepakv Avatar answered Oct 07 '22 00:10

xdeepakv