Error when sending email using nodemailer / smtp.gmail.com - node.js

I'm trying to set up nodemailer for an app. I'm receiving an error when I try it.
This is my setup:
email = setting.email;
password = setting.password;
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '***********#gmail.com', // real email
pass: '*********' // real password
}});
// var transporter = nodemailer.createTransport({
// service: 'gmail',
// auth: {
// user: email, // Your email id
// pass: password // Your password
// }
// });
var mailOptions = {// sender address
from: email,
to: to, // list of receivers
subject: sub, // Subject line
text: text, //, /// plaintext body
html: html
}
//console.log(JSON.stringify(mailOptions));
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.error(error);
} else {
console.log(info.response);
}
;
});
});
} catch (error) {
console.error(error);
}
This is the first time I've tried using nodemailer. I am using real email and password. The error are:
(node:18974) [DEP0025] DeprecationWarning: sys is deprecated. Use util
instead. Magic happens on port 5000 ERROR! ERROR! ERROR! ERROR!
provider_analytic_daily saved. { Error: Connection timeout
at SMTPConnection._formatError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:528:15)
at SMTPConnection._onError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:514:16)
at SMTPConnection. (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:236:14)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5) code: 'ETIMEDOUT', command: 'CONN' }

functionObj = {};
functionsObj.getSmtpTransport = function() {
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "<your email address>",
pass: "<your pass>"
}
});
return smtpTransport;
}
functionsObj.sendMailForPasswordReset = function(to, token) {
var smtpTransport = functionsObj.getSmtpTransport();
var mailOptions = {
to: to,
subject: "Blabla.com Reset Password",
html: "Hi,<br> Click to link for resetting password.<br><a href='https://<blabla.com>/reset/" + urlencode(token) + "'>Click Me !!!</a>"
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
return error;
}
else {
return true;
}
});
};
Hi,
The code above worked in my project without any error. But you should allow to third party softwares to use your google account from google.
Here the google's support page for this case:
https://support.google.com/accounts/answer/6010255?hl=en
I recommend that all input should be validated :) For example if you are taking "to" parameter from user. You should validate "to" parameter is a valid email address ?

Related

response: '535-5.7.8 Username and Password not accepted. Learn more at\n'

My Node js code is sending proper email on localhost but in backend there showing email and password not valid ?
const nodemailer = require("nodemailer");
const sendEmail = async (subject, message, send_to, sent_from, reply_to) => {
// Create Email Transporter
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
// Options for sending email
const options = {
from: sent_from,
to: send_to,
reply_to: reply_to,
subject: subject,
html: message,
};
// Check email sent successfully or not
transporter.sendMail(options, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
};
module.exports = sendEmail;

Unable to send email through nodemailer

I'm developing node.js application in localhost. In this I'm trying to send mail using nodemailer.
Here is the code to used to send mail:
const nodemailer = require('nodemailer');
module.exports = {
sendMail : function( message, htmlMessage, subject, email ) {
const transporter = nodemailer.createTransport( {
service : 'gmail',
host : 'smtp.gmail.com',
auth : {
user : 'username',
pass : 'password'
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from : 'sender mail id', // sender address
to : email, // list of receivers
subject : subject, // Subject line
text : message, // plain text body
html : htmlMessage // html body
};
// send mail with defined transport object
transporter.sendMail( mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
}
};
I'm getting the below error:
Error: connect ETIMEDOUT 74.125.130.108:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ESOCKET',
syscall: 'connect',
address: '74.125.130.108',
port: 587,
command: 'CONN'
}
Kindly someone help me to resolve this issue.
If your firewall is already turned off, you should make these changes.
const nodemailer = require('nodemailer');
module.exports = {
sendMail : function( message, htmlMessage, subject, email ) {
const transporter = nodemailer.createTransport({
service : 'gmail',
auth : {
user : 'username',
pass : 'password'
}
});
// setup email data with unicode symbols
let mailOptions = {
from : 'xyz#gmail.com', // sender address
to : email, // list of receivers
subject : subject, // Subject line
text : message, // plain text body
};
// send mail with defined transport object
transporter.sendMail( mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
}
};
Once you setup, open the link https://myaccount.google.com/lesssecureapps and turn 'ON' Allow less secure apps: ON. And then check by running your node server.

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/

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'});
}

SMTP using nodemailer in nodejs without GMail

I am using node mailer with GMail
smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "myemail ",
pass: "mypass"
}
});
which is working fine, but I want to use my own email server instead of GMail.
This:
smtpTransport = nodemailer.createTransport("SMTP", {
service: "mymailservr link url",
port : 25
auth: {
user: "myemail ",
pass: "mypass"
}
});
It throws this error:
connect ECONNREFUSED
The service option is only for well-known services. To specify your own host, set host.
var smtpTransport = nodemailer.createTransport('SMTP', {
host: 'yourserver.com',
port: 25,
auth: {
user: 'username',
pass: 'password'
}
});
make sure you have the latest NodeMailer version installed
as of today (15-jan-2020) it is v6.4.2
npm install nodemailer --save
This should work:
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.server.com', // <= your smtp server here
port: 2525, // <= connection port
// secure: true, // use SSL or not
auth: {
user: 'userId', // <= smtp login user
pass: 'E73oiuoiC34lkjlkjlkjlkjA6Bok7DAD' // <= smtp login pass
}
});
let mailOptions = {
from: fromEmailAddress, // <= should be verified and accepted by service provider. ex. 'youremail#gmail.com'
to: toEmailAddress, // <= recepient email address. ex. 'friendemail#gmail.com'
subject: emailSubject, // <= email subject ex. 'Test email'
//text: emailData.text, // <= for plain text emails. ex. 'Hello world'
html: htmlTemplate // <= for html templated emails
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('Message sent: %s', info.messageId);
});
Hope this helps,
Thank you.
var nodemailer = require('nodemailer');
var smtpTransport = require("nodemailer-smtp-transport");
var transporter = nodemailer.createTransport(smtpTransport({
host : "example.com",
port: 25,
auth : {
user : "username",
pass : "password"
}
}));

Resources