How to change the "from" field in nodemailer?

Disclaimer: I am not very good at technical aspects of email.

So, I set up a free zoho mail account , which is basically just a mail server for my domain. This view works through mx record forwarding or something else, I'm not quite sure how it works.

In any case, the fact is that I can easily change the field from the field when using my account for Outlook. Therefore, my email address foo@bar.com displayed as Foo from bar.com in most email clients.

Now I want to send some automatic emails from my donotreply@bar.com account using nodemailer (v1.10.0) via SMTP using SSL. I tried different approaches that I found in the documentation / on the Internet. All of them simply threw an ambiguous stack trace (see below).

As soon as I stop trying to change the from field, everything works fine (except for the wrong field). Since I have no idea what is going on, I ask some to help fix this problem.

I tried changing the second argument to createTransport() to my desired from the field. Does not work.

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

to

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

I tried setting auth.mail.from , which also did not work. And I tried to set auth.mail.from with passing the second 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 is the stack I 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) 
+5
source share
3 answers

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

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

The first example you can look at 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); }); 
+4
source

I had the same problem with the from: field, and I went back to configuring gmail, and this is what I found - as I said, only GMAIL. I have not tried another provider yet.
src: nodemailer.com/usage/using-gmail/
Client: Gmail

Gmail also always sets an authenticated username as an email address: address. Therefore, if you are authorized as foo@example.com and set bar@example.com as the from: then address, Gmail returns this and replaces the sender with an authenticated user.

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

Thank you if i'm wrong let me know

+4
source

Source: https://habr.com/ru/post/1236936/


All Articles