Sending Email using nodemailer - node.js

I am trying to send emails to using nodemailer from my site but it gives me this error
Error: Invalid login: 535-5.7.8 Username and Password not accepted.
and I made sure the password and credentials is accurate. This is my code for setting up nodemailer
function message(merchantEmail, msgTxt){
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
secure: true,
port: 465,
auth: {
user: 'kesterdaniel401#gmail.com',
pass: process.env.PASSWORD
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: 'kesterdaniel401#gmail.com',
to: merchantEmail,
subject: 'Sending Email using Node.js',
text: msgTxt
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
module.exports = message
This is where I called the function
message(productOwner.Email, `You have received an order from ${Buyer.Name} for the product ${product. ProductName}. Please contact the customer as soon as possible. Customer Number is ${Buyer.PhoneNumber}.`)

Related

How to send mail using Nodemailer in Node.js

I tried to send e-mail using Nodemailer in Node.js but it is not working. I don't know why it is not working. If anyone knows, please help, to find the solution.
Getting this error:
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials 9sm14519585pfh.160 - gsmtp
data.controller.js:
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mygmail#gmail.com',
pass: 'mypass'
}
});
let mailOptions = {
from: "mygmail#example.com", // sender address
subject: "Hello ✔", // Subject line
text: "Hello This is an auto generated Email for testing from node please ignore it", // plaintext body
to: "togmail#gmail.com"
}
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
const nodemailer = require('nodemailer');
const email = 'myemail#gmail.com';
const password = '**********';
You should set smtp host as smtp.gmail.com.
You can set smtp port number as 587 or 465.
587 is tls and 465 is ssl.
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: `${email}`,
pass: `${password}`
}
});
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
var mail = 'message';
const from_email = 'sender#email.com';
const to_email = 'receiver#email.com';
var mailOptions = {
from: email,
to: to_email,
subject: 'Your subject',
text: mail
};
}
});
I used above code, so that I sent mail.
Please try this.

Nodemailer - Missing credentials for "PLAIN"

I am working with node.js and tried sending email via Nodemailer but I'm getting this error:
Missing credentials for "PLAIN"
function(token, user, done) { //sends mail
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'aimanmumtazxyz#gmail.com',
pass: process.env.GMAILPW
}
});
Any help anybody?
const nodemailer = require("nodemailer");
async function main() {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: userName, // Enter your userName or email
pass: password // Enter your password
}
});
// send mail with defined transport object
await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "bar#example.com, baz#example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>" // html body
}, (err, info)=>{
if (err) {
throw new Error(err)
} else {
console.log('Email sent: ' + info.response);
}
});
For more information about nodemailer, please check this link: https://nodemailer.com/about/
For sending an email by Gmail, please check this link: https://nodemailer.com/usage/using-gmail/

Send mail with nodejs

For a school project I have to build a website and all the stuff...
I want to send an email when a certain button is pressed. For now I used an gmail address for the server BUT it needs authentification and all. How can I bypass the authentification ? Are there some other STMP servers that do not require authentification so I send an email easily ?
Thanks guys !
You should use Nodemailer
Its a npm module installed it and there you go.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail#gmail.com',
pass: 'yourpassword'
}
});
var mailOptions = {
from: 'youremail#gmail.com',
to: 'myfriend#yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Refer : https://www.w3schools.com/nodejs/nodejs_email.asp
var transporter = nodemailer.createTransport({
host:'smtp.gmail.com',
port:587,
secure:false,
auth: {
user: 'youremail#gmail.com',
pass: 'here app password'
}
});
url=`http://localhost:3000/verify-email/${token}`;
let mailDetails={
from:'"verify Your Email"< youremail#gmail.com>',
to:user.email, //receiver email address
subject:'Register Verify Your Email',
html:`<h2>${user.fullname} thanks for register on our site </h2>
<h4>please verfiy your mail to continue....</h4>
verfiy your Email`
}
transporter.sendMail(mailDetails,function(err,data){
if(err){
console.log('error ocures..',err)
}else{
console.log('verfify email is sent to your account');
}
})

Unable to send email through node js, tried few methods

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
secureConnection: false,
port: 587,
tls: {
ciphers: 'SSLv3'
},
requireTLS: true,
auth: {
user: 'mygmail',
pass: 'mypass'
}
});
var mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error)
return console.log(error);
console.log('Email sent: ' + info.response);
});
Above is my code for sending email through node js but I keep encounter a timeout error as below
{ Error: connect ETIMEDOUT 74.125.24.109:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: '74.125.24.109',
port: 587,
command: 'CONN' }
I also tried another basic way as for the following link: https://www.w3schools.com/nodejs/nodejs_email.asp but it is not working as well.
I've already turned on allow the less secure app on my google account.
Beside of that, I also tried the method like the one in the following the link: https://jsfiddle.net/burawi/1u9m2mou/ and it is still unable to work, I generated all the client_id, client_secret, access_token, and refresh_token.
Does anyone have any latest guide or solution for sending email through node js?
Thanks
This is a network restriction issue.
Please try the same thing in your mobile network and it will work.
router.get('/mailTest', (req, res) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "mrmanagementbbsr#gmail.com", // generated ethereal user
pass: "Test#123" // generated ethereal password
}
});
let mailOptions = {
from: "mrmanagementbbsr#gmail.com", // sender address
to: 'nsubhadipta#gmail.com', // list of receivers
subject: "test subject", // Subject line
text: 'demo text'
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.json({ status: -1, message: 'Error Occured', error: error });
}
else {
res.json({ status: 1, message: "Email Sent" });
}
});
});
Ensure you turn on your Less secure app access, else your emails will not go through.
Then you might want to try this example:
{
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mymail',
pass: 'mypass'
}
});
let mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info)=>{
if (error) {
console.log(error);
} else {
console.log('Email sent ' + info.response');
}
});
res.json({success: 'whatever message you plan on writing'});
}

sending email via nodemailer not working

I have a simple piece of code which is not working as expected. I am using nodemailer.
var nodemailer = require('nodemailer');
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: 'personal gmail address',
pass: 'password'
}
});
var mailOptions = {
from: 'personal gmail address',
to: 'personal gmail address',
subject: 'Hello world!',
text: 'Plaintext message example.'
};
smtpTransport.sendMail(mailOptions, function(err) {
console.log('Message sent!');
});
I am getting the Message Sent in console. But no emails in inbox.
For gmail service I use this :
const smtpTransport = require('nodemailer-smtp-transport')
const nodemailer = require('nodemailer')
const transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'email',
pass: 'pass'
}
}))
But you need to allow less secure authentication on your gmail account or emails are not sent.
I think the steps are :
Go to : https://www.google.com/settings/security/lesssecureapps
set the Access for less secure apps setting to Enable
Found this SO: self signed certificate in certificate chain error in mail .
I needed to add this and it worked for me.
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
Can you check for the error message.
...
smtpTransport.sendMail(mailOptions, function(err) {
if(err)
{
console.log(err);
}
else
{
console.log('Message sent!');
}
});
Because in the sample code you had, even if the nodemailer returns valid error, it will still print Message Sent
Some times message can be queued or say any type of error in the mailOptions, the message cant be delivered.

Resources