Reputation: 742
I'm trying to send mails with nodejs and nodemailer, but it doesn't work for me. I have this code:
const config = require('../../config');
const http = require('http');
const nodemailer = require('nodemailer');
module.exports = {
sendMail: function (params) {
return new Promise((resolve, reject) => {
var transporter = nodemailer.createTransport({
//service: 'gmail', //I tried with this too
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: params.fromMail,
pass: params.fromPass
}
});
var mailOptions = {
from: params.fromMail,
to: params.toMailList,
subject: params.subject,
html: params.mailHtmlContent,
};
transporter.sendMail(mailOptions, function(err, info){
if (err) {
reject(err);
} else {
resolve(info.response);
}
});
});
}
};
I'm 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 p6sm97493129wrx.50 - gsmtp at SMTPConnection._formatError (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:774:19) at SMTPConnection._actionAUTHComplete (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:1509:34) at SMTPConnection._responseActions.push.str (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:547:26) at SMTPConnection._processResponse (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:933:20) at SMTPConnection._onData (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:739:14) at TLSSocket._socket.on.chunk (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:691:47) at TLSSocket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at TLSSocket.Readable.push (_stream_readable.js:219:10) code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCredentials p6sm97493129wrx.50 - gsmtp', responseCode: 535, command: 'AUTH PLAIN' }
I have changed the correspondant param on my gmail account about "less secure apps". I tried with another non gmail account too. I tried with this:
service: 'gmail',
and this for the connection:
host: 'smtp.gmail.com',
port: 465,
secure: true,
I read all "Similar Questions" here in stackoverflow and I don't know if I need anything else or if I'm doing anything bad. (of course I have revieved the username and password in both accounts and they're ok).
What am Im doing bad?
Upvotes: 3
Views: 13244
Reputation: 1003
The accepted answer did not work for me. These four steps did:
Sign in to your Google Admin console using an administrator account.
From the Admin console dashboard, go to Security and then Basic settings (you might have to click More controls at the bottom).
Under Less secure apps, select Go to settings for less secure apps.
In the subwindow, select the Enable access to less secure apps for all users radio button.
Upvotes: 2
Reputation: 439
var nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',//smtp.gmail.com //in place of service use host...
secure: false,//true
port: 25,//465
auth: {
user: '[email protected]',
pass: 'password'
}, tls: {
rejectUnauthorized: false
}
});
transporter.sendEMail = function (mailRequest) {
return new Promise(function (resolve, reject) {
transporter.sendMail(mailRequest, (error, info) => {
if (error) {
reject(error);
} else {
resolve("The message was sent!");
}
});
});
}
module.exports = transporter;
Usage:
//import transporter JS file
const mail = require('../utils/mail');
let htmlContent = `
<h1><strong>Contact Form</strong></h1>
<p>Hi,</p>
<p>${name} contacted with the following Details</p>
<br/>
<p>Email: ${email}</p>
<p>Phone: ${phone}</p>
<p>Company Name: ${companyName}</p>
<p>Message: ${message}</p>
`
let mailOptions = {
from: "Example <[email protected]>",
to: "[email protected]",
subject: "Mail Test",
text: "",
html: htmlContent
}
mail.sendMail(mailOptions)
.then(function (email) {
res.status(200).json({ success: true, msg: 'Mail sent' });
}).catch(function (exception) {
res.status(200).json({ success: false, msg: exception });
});
Note:
Before sending your email using gmail you have to allow non secure apps to access gmail you can do this by going to your gmail settings https://myaccount.google.com/lesssecureapps?pli=1
Also enable the captcha https://accounts.google.com/DisplayUnlockCaptcha
Upvotes: 8