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]'
}
});
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.
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”.
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.
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')
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With