I tried to send e-mail using Node.js Nodemailer but it is not working. I do not know how to set up it properly. I have used below code but it is not working and I'm getting error. I am new to Nodemailer, so I'm not able to find out the mistake.
Allow less secure apps: ON
What do I need to do from google account? Please help anyone to find a solution.
app.js:
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const app = express();
// View engine setup
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.engine('handlebars', exphbs({
defaultLayout: false,
}));
//app.locals.layout = false;
app.get('/', (req, res) => {
res.render('contact');
});
app.post('/send', (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Company: ${req.body.company}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'mail.madepartooty.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'myemail@gmail.com', // generated ethereal user
pass: 'test123' // generated ethereal password
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <myemail@gmail.com>', // sender address
to: 'toemail@gmail.com', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // 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));
res.render('contact', { msg: 'Email has been sent' });
});
});
app.listen(3000, () => console.log('Server started...'));
Error:
Error: connect ETIMEDOUT 157.245.154.37:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: '157.245.154.37',
port: 587,
command: 'CONN'
}
You can do the following
app.js
var nodemailer = require('nodemailer');
var tech = require('./external.js');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: tech.senderUsername,
pass: tech.senderPassword
}
});
const mailOptions = {
from: tech.senderUsername, // sender address
to: tech.reciverUsername, // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Enter your message here.</p>'// plain text body
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
});
Note :
You may also need to follow these steps while using google account
Enable the settings to allow less secure apps for the Gmail account that you are using. Here is the link: Google Less Secure Apps
Allow access for "Display Unlock captcha option" (Allow access to your Google account) Here is the link: Google Unlock Captcha
For more info, you can go through sendMailUsingNodemailer