Nodemailer with Gmail “From: address” does not cha

2019-07-15 06:36发布

I am trying to send emails to any new subscriber to my mailing list. Everything is working great, but the "sender" email address does not change to "noreplay. It stays as the email I authenticated with, in this case my work mail. For example, if I set the "from" to "noreply@example.com" it sends the mail from "jaafar@example.com". This is my code:

if (snapshot.child("subscribed").val() === 'true') {
  var value = snapshot.child("email").val(); //ignore this
  var key = snapshot.key;  //ignore this

  var mailHtml = val.htmlText1 + httpLink + key + val.htmlText2; //ignore this
  let mailOptions = {
   from: '"JaafarsCompany" <noreply@jaafarsCompany.io>', // sender address
   to: value, // list of receivers
   subject: 'Hello', // Subject line
   text: 'Hello world', // plain text body
   html: mailHtml //ignore this
  };

  // send mail with defined transport object
  mailTransport.sendMail(mailOptions, (error, info) => {
   if (error) {
     return console.log(error);
   }
  console.log('Mail sent to: ' + value + '. ' + 'Message %s sent: %s', info.messageId, info.response);
});

I have only built upon the example from the documentation. The received email comes as if it was from "JaafarsCompany", but if you click on the sender-name, it shows my work mail.

Hope that makes sense. I am looking for anything that can point me in the right direction. I feel like I have been googling this problem for ages :(

1条回答
Viruses.
2楼-- · 2019-07-15 07:00

I figured out what the problem was. Essentially the doc says:

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.

To work around this issue, one needs to add a group in the Admin Console. After that go to this link, and follow the instructions.

I had help from a Firebase Supporter to fix this issue. Hope this helps somebody one day :-)

查看更多
登录 后发表回答