18

I'm trying to send email using Nodejs package Nodemailer, but i'm unable to change the from email to my custom domain email. Any help will be appreciated. here is the code i'm using to send an email.

transporter.sendMail({
   from: 'support@domain.com',
   to: 'recipient@example.com',
   subject: 'Message',
   text: 'I hope this message gets through!',
   auth: {
            user: 'zeshanvirk@gmail.com'
         }
});
Zeshan Virk
  • 183
  • 1
  • 1
  • 7
  • According to the documentation (https://nodemailer.com/message/) there is a 'sender' attribute that you can try with. Also, can you post the content of the headers of the mail you're receiving? – javierfdezg Apr 17 '18 at 06:37
  • i used the sender attribute like sender: 'support@domain.com', but again i received email from zeshanvirk@gmail.com which is the user in Auth. – Zeshan Virk Apr 18 '18 at 07:13

2 Answers2

13

Just like it is pointed out in the nodemailer documentation https://nodemailer.com/smtp/

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.

var transporter = nodemailer.createTransport({
    host: 'something.yourdomain.com',
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: 'username@mydomain.com', // your domain email address
      pass: 'password' // your password
    }
  });

Then you can go on and define the mail options:

  var mailOptions = {
    from: '"Bob" <bob@bobsdomain.com>',
    to: 'tom@gmail.com',
    subject: "Hello",
    html : "Here goes the message body"
  };

Finally you can go on and use the transporter and mailOptions to send an email using the function sendEmail

  transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
      console.log(err);
      return ('Error while sending email' + err)
    }
    else {
      console.log("Email sent");
      return ('Email sent')
    }
  });
FernandoTN
  • 141
  • 6
  • Hi there! I get the message 'email sent' and the function finishes with the status 'ok'. But the receiver does not receive the email. Can you help please :( – Adil Naseem Jan 30 '22 at 10:22
  • Ensure the email is being sent to a real email address. For example I was using Firebase DB and added it there to the new entry – Robgit28 Feb 06 '23 at 22:36
0

Also make sure that the user in your auth is the same the user you are sending the mail from