Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom domain email in nodemailer?

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: '[email protected]',
   to: '[email protected]',
   subject: 'Message',
   text: 'I hope this message gets through!',
   auth: {
            user: '[email protected]'
         }
});
like image 334
Zeshan Virk Avatar asked Apr 17 '18 04:04

Zeshan Virk


People also ask

How can I send email with my company domain using Nodemailer?

In short, what you need to do to send messages, would be the following: Create a Nodemailer transporter using either SMTP or some other transport mechanism. Set up message options (who sends what to whom) Deliver the message object using the sendMail() method of your previously created transporter.

How do I send and receive emails with custom domain?

Guide to adding a custom Sending DomainHead to Settings > Current Project > Email. Hit the “Add a new Sending Domain” button. Enter the domain name you wish to send email from (for example, if you want to send email from [email protected], you'd add “example.com” as your domain name). Then hit “Add”.

How do I send an HTML email with Nodemailer?

In server. js , add the following code, which will trigger sending an email: const hbs = require('nodemailer-express-handlebars') const nodemailer = require('nodemailer') const path = require('path') // initialize nodemailer var transporter = nodemailer.


1 Answers

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: '[email protected]', // your domain email address
      pass: 'password' // your password
    }
  });

Then you can go on and define the mail options:

  var mailOptions = {
    from: '"Bob" <[email protected]>',
    to: '[email protected]',
    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')
    }
  });
like image 127
FernandoTN Avatar answered Oct 15 '22 09:10

FernandoTN