I want to send an email from node backend and I am using the nodemailer library for express to achieve that. I have written the following code to configure the transport and other details for nodemailer
let transport = nodemailer.createTransport({
host: 'mail.myrandomhost.in',
port: 587,
tls: {
rejectUnauthorized:false
},
secure: false,
auth: {
user: 'me#myemail.in',
pass: 'Gr3aseMonk3y'
}
});
const message = {
from: 'me#myemail.in',
to: 'youremail#gmail.com',
subject: 'test email',
html: '<h1>Hey</h1><p>This is test <b>Tesla</b> email</p>'
};
transport.sendMail(message, function(err, info) {
if (err) {
console.log('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=');
console.log("FAILED");
console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
console.log(err)
} else {
console.log('====================================');
console.log("Email sent");
console.log('====================================');
console.log(info);
}
});
When I run this, I get the following error
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FAILED
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Error: Invalid login: 535 Incorrect authentication data
at SMTPConnection._formatError (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._actionAUTHComplete (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:1523:34)
at SMTPConnection.<anonymous> (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:550:26)
at SMTPConnection._processResponse (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:942:20)
at SMTPConnection._onData (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (/Users/sarthak/Desktop/CodingEnvironments/NodeJS/templates/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
code: 'EAUTH',
response: '535 Incorrect authentication data',
responseCode: 535,
command: 'AUTH PLAIN'
}
I am using the correct details. How do I solve this?
You entered the wrong data. you must change host, from to real addresses.
Example:
let transport = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
tls: {
rejectUnauthorized:true
},
secure: false,
auth: {
user: 'yourRealEmail#gmail.com',
pass: 'yourPassword'
}
});
const message = {
from: 'yourRealEmail#gmail.com',
to: 'RealEmailAddress#gmail.com',
subject: 'test email',
html: '<h1>Hey</h1><p>This is test <b>Tesla</b> email</p>'
};
transport.sendMail(message, function(err, info) {
if (err) {
console.log('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=');
console.log("FAILED");
console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-');
console.log(err)
} else {
console.log('====================================');
console.log("Email sent");
console.log('====================================');
console.log(info);
}
});
Related
I'm trying to send mail from rediffmail using nodemailer
Here's the code snippet
var transporter = nodemailer.createTransport({
host: "smtp.rediffmail.com", // hostname
port:25, secureConnection: false,
secure: false,
tls: {
ciphers: "SSLv3",
},
auth: {
user: process.env.MAILADDRESS,
pass: process.env.MAILPASS,
},
});
var mailOptions = {
from: process.env.MAILADDRESS,
to: email,
subject: "Sending using Node.js",
html: "<h1>hoohaa</h1>That was eafefesy!",
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log(err);
} else {
console.log(
`Password recovery Email sent to ${email} : ${info.response}`
);
}}
and the error which I get is :
{ errno: -4039, code: 'ESOCKET', syscall:
'connect', address: '202.137.235.17', port: 25, command:
'CONN' }
Maybe you could use smtp.gmail.com ?
Remove the TLS. You're not using ssl so you don't need it.
I have a business email using bell.net email address, and on their site, it says use smtphm.sympatico.ca, as the smtp hostname, as well as port 25 / 587... I have my nodemailer code set up like this:
app.post('/sendBatchEmail', (req, res) => {
var emails = [];
var emailSubject = req.body.emailSubject;
var emailMessage = req.body.emailMessage;
//perform db2 send
var sendEmail = "select * from testEmails"
ibmdb.open(ibmdbconnMaster, function (err, conn) {
if (err) return console.log(err);
conn.query(sendEmail, function (err, rows) {
if (err) {
console.log(err);
}
for (var i = 0; i < rows.length; i++) {
emails.push(rows[i].EMAIL)
}
//send email
async function main() {
let transporter = nodemailer.createTransport({
host: "smtphm.sympatico.ca",
port: 25,
secure: false, // true for 465, false for other ports
auth: {
user: "xxx#bell.net",
pass: "xxx",
},
});
// send mail with defined transport object
let sendBatch = await transporter.sendMail({
from: "my1email#bell.net", // sender address
to: "myemail#gmail.com",
bcc: emails, // list of receivers
subject: emailSubject, // Subject line
text: emailMessage, // plain text body
});
console.log("Message sent: %s", sendBatch.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
// console.log("Preview URL: %s", sendBatch.getTestMessageUrl);
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
res.redirect("/");
conn.close(function () {
console.log("closed the function app.get(/sendEmailBatch)");
});
});
});
})
however, this doesn't work and gives me this error:
Error: Invalid login: 535 Authentication failed
at SMTPConnection._formatError (/Users/ga/Desktop/PSL/mbs/node_modules/nodemailer/lib/smtp-connection/index.js:774:19)
at SMTPConnection._actionAUTHComplete (/Users/ga/Desktop/PSL/mbs/node_modules/nodemailer/lib/smtp-connection/index.js:1513:34)
at SMTPConnection.<anonymous> (/Users/ga/Desktop/PSL/mbs/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
at SMTPConnection._processResponse (/Users/ga/Desktop/PSL/mmbs/node_modules/nodemailer/lib/smtp-connection/index.js:932:20)
at SMTPConnection._onData (/Users/ga/Desktop/PSL/mbs/node_modules/nodemailer/lib/smtp-connection/index.js:739:14)
at TLSSocket.SMTPConnection._onSocketData (/Users/ga/Desktop/PSL/mbs/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at TLSSocket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
code: 'EAUTH',
response: '535 Authentication failed',
responseCode: 535,
command: 'AUTH PLAIN'
}
but works when I change it back to gmail... any ideas?
I'm using nodemailer with npm package email-templates. I'm not getting email. Template is working when i set preview:true. Getting error on console Error: No recipients defined.Why this error coming? Tried many things but nodemailer is sending empty email every time. Hope you understand my issue.
Error:
Error: No recipients defined
at SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
at SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
at SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
at sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
at E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
at SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
at SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
at SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
at SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
code: 'EENVELOPE',
command: 'API'
}
My directory structure:
├── app.js
└── emails
└── forget-password
├── html.pug
├── subject.pug
Node mailer with template:
const nodemailer = require('nodemailer');
var generator = require('generate-password');
const Email = require('email-templates');
exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
});
const emailVar = new Email({
message: {
from: 'testing#gmail.com'
},
preview: true,
send: true,
transport: {
jsonTransport: true
}
});
emailVar.send({
template: 'forget-password',
message: {
to: email
},
locals: {
password: GeneratePassword
}
}) .then(res => {
console.log('res.originalMessage', res.originalMessage)
}).catch(console.error);
return transporter.sendMail(emailVar, function (err, info) {
if (err)
console.log(err);
else
console.log(info);
});
};
Html.pug:
p Your new password is: #{password}
subject.pug
= `Password reset request`
the reason is that you dont need to require nodemailer instead of make your transporter with nodemailer you need to pass it in JSON like so.
exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
});
becomes:
transport: {
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}
}
const Email = require('email-templates'); <-- with this is enough.
If anyone else is looking how to solve this, i manage to make it work based on #Irving Caarmal answer and the code is the following:
function sendTokenEmail(userEmail, token, templateName) {
const email = new Email({
message: {
from: 'someemail#gmail.com',
},
preview: true,
send: true,
transport: {
host: process.env.MAIL_HOST,
port: process.env.MAIL_PORT,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
},
})
return email
.send({
template: templateName,
message: {
to: userEmail,
},
locals: {
token,
},
})
.then((res) => {
console.log('res.originalMessage', res.originalMessage)
})
}
if anyone has an issue with getting on their email address the mail generated you should add at the level with send: true also the option preview: false (PS: this worked for me) without having the preview set to false the email would not come even if the docs say:
If you want to send emails in development or test environments, set options.send to true.
Example:
const email = new Email({
message: {
from: process.env.SMTP_USER,
},
send: true,
preview: false,
transport: {
host: process.env.SMTP_HOST,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
},
})
await email.send({
template: 'template-of-choice',
message: {
from: 'email-address'
},
locales: {}
})
Hope it helps :)
let transporter = nodemailer.createTransport({
host: 'smtp.googlemail.com',
port: 465,
secure: true,
auth: {
user: '*******#gmail.com', //Gmail username
pass: '******' // Gmail password
},
tls:{
rejectUnauthorized: false
}
});
let mailOptions = {
from: ' "Nodemailer Contact" <********#gmail.com> ',
to: '********#gmail.com',
subject: 'Node Contact Request',
text: '.......',
html: output
};
transporter.sendMail(mailOptions, (err, info)=>{
if(err) {
return console.log(err);
}
console.log("Message Sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
res.render('contact', {msg: 'Email has been Sent!'}, {layout: false});
//res.render('contact', {layout: false});
});
ERROR:
TypeError: callback is not a function
at Immediate._onImmediate (/home/sDesktop/EmailPage/node_modules/express-handlebars/lib/utils.js:18:13)
at runCallback (timers.js:794:20)
at tryOnImmediate (timers.js:752:5)
at processImmediate [as _immediateCallback] (timers.js:729:5)
I am trying to send an email after registration to a user. But I am having an issue with requiring of nodemailer module. I am getting the following error:
-The version of my npm is: 6.2.0
-The version of my node is: v8.11.1
-My package.json looks like this:
-My function for sending email looks like this:
sendEmail() {
var nodemailer = require("nodemailer");
nodemailer.createTestAccount((err, account) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'example#gmail.com',
pass: 'example'
}
});
let mailOptions = {
from: '"Example" <example#gmail.com>',
to: this.inputEmail.value,
subject: 'Successful registration!',
text: "Thank you registing with us!"
};
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));
})
})
}