Nodemailer - Missing credentials for "PLAIN" - node.js

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/

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;

Sending Email using nodemailer

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}.`)

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.

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.

Nodemailer without Gmail

I'm using NodeJS and I want to configure my email account with Nodemailer.
All the Nodemailer examples that I found are for Gmail...
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'userpass'
}
});
How can I put another service? In special I bought a email domain in ovh (SSL0.OVH.NET) and I interested to configure this email account.
I tried but I don't found the way to get this...
Thank you!
I create a module Mail for send html mails by SMTP
exports.send = function(email, subject, htmlcontent, callback) {
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var configMail = require('../bin/config').mail;//my json configurations mail
var transporter = nodemailer.createTransport(smtpTransport({
host: configMail.host, //mail.example.com (your server smtp)
port: configMail.port, //2525 (specific port)
secureConnection: configMail.secureConnection, //true or false
auth: {
user: configMail.auth.user, //user#mydomain.com
pass: configMail.auth.pwd //password from specific user mail
}
}));
var mailOptions = {
from: configMail.email,
to: email,
subject: subject,
html: htmlcontent
};
transporter.sendMail(mailOptions, function(err, info){
transporter.close();
if(err) {
callback(err, info);
}
else {
callback(null, info);
}
});
}
The usage:
var Mail = require('../utils/Mail'); //require this module
Mail.send('[EMAIL TO SEND]', '[TITLE]', '[YOUR HTML TEMPLATE MAIL]', function(err, info) {
if(err) {
//error
}
else {
//Email has been sent and you can see all information in var info
}
});
On creating an email id in the domain (mostly through c panel), check the mail configurations for that mail address and look for SMTP details such as port, username, etc. Then enter those details in the nodemailer transporter. For example, my transporter details were:
const transporter = nodemailer.createTransport({
host: "mail.schoolprogramming.tech",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: "demo#schoolprogramming.tech",
pass: "**************"
}
});

Resources