Sending email from own server to the internets - javascript

I have set up an email server on my host. It's basically a SMTP server that listens on port 25.
const recvServer = new SMTPServer({
requireTLS: true,
authOptional: true,
logger: true,
onConnect(session, callback) {
return callback();
},
onMailFrom(address, session, callback) {
console.log('from', address, session);
return callback();
},
onData(stream, session, callback) {
console.log('new msg');
let message = '';
stream.on('data', chunk => {
message += chunk;
});
stream.on('end', () => {
callback(null, 'Message queued');
simpleParser(message)
.then(parsed => {
console.log(parsed);
// here I wish to forward the message to outside gmail addresses
})
.catch(err => {
console.log(ee)
});
});
}
});
recvServer.listen(25);
recvServer.on('error', err => {
console.log(err.message);
});
It works fine for receiving emails from outside, like gmail etc.
But I want to be able to send emails outside also, or forward emails that I receive to some gmail addresses.
I know that I can do that using Gmail SMTP servers, but then I need an gmail account and password.
I want to be able to send email with my own server, just like yahoo sends mail to gmail using their own server not gmail accounts :)

You need a MTA (Mail Transfer Agent) in order to send an email.
So the popular options is: Postfix, here a guid how to setup postfix on ubuntu: https://help.ubuntu.com/community/Postfix
Or you can spin up a docker container like: https://hub.docker.com/r/bytemark/smtp/
Then you can use nodemailer to send emails through postfix or docker instance.
And if you want there is a full stack docker image all batteries included: https://github.com/tomav/docker-mailserver

Technically you can use NodeMailer for sending emails.
"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main(){
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
});
// send mail with defined transport object
let info = 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
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

As Sohail mentioned in the above answer you can use NodeMailer (https://nodemailer.com/about/) and its fairly easy. Below is a simple code snippet of using it. In addition there are other services that provided free tiers if you are looking for fancier services(examples are SendGrid (https://sendgrid.com/)).
async function sendemail(email, host , email_body) {
let transporter = nodemailer.createTransport({
host: "mail.example.com.au",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: "test#example.com.au", // generated ethereal user
pass: "email_password" // generated ethereal password
}
});
let emailBody = email_body // This is the body of your email which is more or less a String
let info = await transporter.sendMail({
from: '" Name of sender" <test#example.com.au>', // sender address
to: email, // list of receivers
subject: "Email Subject", // Subject line
text: emailBody, // plain text body
// html: // html body
});
console.log("Message sent: %s", info.messageId);
return true;
}

In order to send email, you need an email client, not a server. Writing the client is usually easier than writing the server, so you could do that. If you don't want to write your own, you could use an MTA (like Postfix), which will contain both a client and a server.

You can solve this function by creating an account in SendGrid and then using its Node.js api to send emails.
These are the steps:-
npm install #sendgrid/mail
And then use this following code snippet :-
const mailer = require('#sendgrid/mail');
mailer.setApiKey(<YOUR_SENDGRID_API_KEY>);
const message = {
to: 'xyz#gmail.com',
from: 'abc#gmail.com',
subject: 'hello world',
text: 'hello world'
};
mailer.send(message);
You can get more samples and documentation in their Github repo.

Related

nodemailer timing out when trying to send email

I have this:
app.post('/sendSessionConformationEmail', (req, res) => {
if (!req.session.loggedin) {
res.redirect('/login')
req.session.destroy()
}
var email = req.body.email;
var name = req.body.name;
var trainername = req.body.trainername;
var sessionDate = new Date().toLocaleString();
console.log(email)
console.log(name)
console.log(trainername)
console.log(sessionDate)
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// 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: "myemail#gmail.com",
pass: "myauthpassword",
},
tls:{
rejectUnauthorized:false
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: 'my2email#gmail.com', // sender address
to: "gianluca#my3email.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
})
I copied the code straight from the nodemailer website, and made the necessary changes. However, when I run this code, nothing sends, and it actually gives me this error:
Error: connect ETIMEDOUT 13.49.22.0:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -60,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '13.49.22.0',
port: 443,
type: 'FETCH',
sourceUrl: 'https://api.nodemailer.com/user'
}
I've been using nodemailer with this code for over 2 years, never gotten this error, and it is killing me. Is anyone able to help?
you should allow third party apps use your gmail.
https://support.google.com/accounts/answer/3466521?hl=en
ethereal.email (13.49.22.0) is down today. Runs on AWS.

how to send emails node mailer without smtp

I am trying to send emails through nodemailer, and I've used it multiple times with my gmail account... the problem is, I don't want to use my gmail account now, and I want to use my business email so I could send out emails to clients on a regular...
I have it set up like this right now, but not sure how to do it without gmail / smtp:
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() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// 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: "x",
pass: "x",
},
});
// send mail with defined transport object
let sendBatch = await transporter.sendMail({
from: "", // sender address
to: "xxxxx#gmail.com",
bcc: emails, // list of receivers
subject: emailSubject, // Subject line
text: emailMessage, // plain text body
});
console.log("Message sent: %s", sendBatch.messageId);
}
main().catch(console.error);
res.redirect("/index");
conn.close(function () {
console.log("closed the function app.get(/account)");
});
});
});
I am not sure how to not use smtp server so I can use biz email, or even if this is possible! Thanks in advance for help :)
})
As you can do this using nodemailer, but sending an email without a SMTP is not recommended as there are much higher chances of being rejected or put into spam folder. I'm pretty sure gmail like services will put the emails in Spam folder. here is how you can implement it:
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "x",
pass: "x",
},
});
// send mail with defined transport object
let sendBatch = await transporter.sendMail({
from: "<foo#example.com>", // sender address
to: "xxxxx#gmail.com",
bcc: emails, // list of receivers
subject: emailSubject, // Subject line
text: emailMessage, // plain text body
});
console.log("Message sent: %s", sendBatch.messageId);
}
For more details you can check here1 and here2

How to send mail without using email-credentials via nodemailer npm in nodejs?

I know how to send mail using nodemailer but I am using email credentials of that, but I don't want to use that. Is there any other method of sending mails.
My nodemailer code snippet is
var nodemailer = require('nodemailer');
// Not the movie transporter!
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'vikaskohli#email.com', // Your email id
pass: 'password' // Your password
}
});
module.exports =
{
sendEmail: function sendMail(varhtml,vartext, varsubject,varfrom, varfrom_name,varto, varto_name, reply_to_email ) {
//setup e-mail data with unicode symbols
var mailOptions = {
from: varfrom_name, // sender address
// to: ['vikaskohli#email.com','vikaskohli1#email.com'], // list of receivers
// to: ['vikaskohli#email.com,vikaskohli1#email.com'], // list of receivers
// to: 'vikaskohli#email.com','vikaskohli1#email.com', // list of receivers
to: varto, // list of receivers
subject: varsubject, // Subject line
text: vartext, // plaintext body
html: varhtml // html body
};
console.log(mailOptions);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}else{
return console.log(info);
}
});
}
}
In the above code, also I don't change my sender name. If I want to send from other mail like
vikaskohli3#gmail.com
but it automatically send from that mail for which I have use credentials here
vikaskohli#email.com
Also I tried using sendmail npm, they don't need any credentials but it send mail in the spam folder
My sendmail code snippet
var sendmail = require('sendmail')({silent: true})
sendmail({
from: 'vikaskohli#email.com',
to: 'vikaskohli#email.com,vikaskohli1#email.com',
subject: varsubject, // Subject line
html: varhtml,
attachments: [
]
}, function (err, reply) {
console.log(err && err.stack)
console.dir(reply)
});
I use sendmail like your bottom code, and it works fine for me. Perhaps the reason it's going to spam is because your settings in the email provider. Most likely the email you chose as sender (#email.com) seems like a spam to that service provider or your settings.

Use smtp client to send email without providing password

I'm using nodemailer to try to send an email to myself via commandline:
var nodemailer = require('nodemailer');
// config
var smtpConfig = {
host: 'smtp.myhost.com',
port: 465,
secure: false, // dont use SSL
tls: {rejectUnauthorized: false}
};
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpConfig);
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Fred Foo 👥" <foo#blurdybloop.com>', // sender address
to: 'person#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
When I try to run this code I get the following error:
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/answer/14257
The link takes you to a page that tells you to register your app inside Google Console. But this is not what I'm trying to do.
There are loads of email clients that can send an email to a gmail inbox without having to sign into that email account. This is what I'm trying to do. I'm trying to turn my terminal into an smtp client that can send a mail message to any inbox. This shouldn't require extensive authentication. How do I do this?
NOTE
Just to provide some perspective, I'm trying to replicate in node whats possible with the unix sendmail command:
sendmail person#gmail.com < testemail.txt
How can I do this using nodemailer?
Try port 587.
Here are my gmail settings.
smtpServerName="smtp.gmail.com"
portNumber="587"
authenticationMode="SSL"
smtpUserName="somebody#gmail.com"
smtpUserPassword="xxxxxxxxxx"
I would experiment with ssl: false and ssl: true
nodemailer.SMTP = {
host: 'smtp.gmail.com',
port: 587,
ssl: false,
use_authentication: true,
user: 'my.username#gmail.com',
pass: 'my.password'
}
Make sure you read this:
https://support.google.com/accounts/answer/6010255
It looks like there are two ways with gmail.
Port 587 is (non ssl) but with TLS.
Port 465 is SSL (but not TLS).
Go figure.
//singleServer = new SmtpServerSettings("smtp.gmail.com",
// "myname#gmail.com", "587", AuthenticationType.Basic,
// "myname#gmail.com", "mypassword", "");
//singleServer = new SmtpServerSettings("smtp.gmail.com",
// "myname#gmail.com", "465", AuthenticationType.SSL,
// "myname#gmail.com", "mypassword", "");

Nodemailer with Gmail and NodeJS

I try to use nodemailer to implement a contact form using NodeJS but it works only on local it doesn't work on a remote server...
My error message :
[website.fr-11 (out) 2013-11-09T15:40:26] { [AuthError: Invalid login - 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvlX
[website.fr-11 (out) 2013-11-09T15:40:26] 534-5.7.14 V-dFQLgb7aRCYApxlOBuha5ESrQEbRXK0iVtOgBoYeARpm3cLZuUS_86kK7yPis7in3dGC
[website.fr-11 (out) 2013-11-09T15:40:26] 534-5.7.14 N1sqhr3D2IYxHAN3m7QLJGukwPSZVGyhz4nHUXv_ldo9QfqRydPhSvFp9lnev3YQryM5TX
[website.fr-11 (out) 2013-11-09T15:40:26] 534-5.7.14 XL1LZuJL7zCT5dywMVQyWqqg9_TCwbLonJnpezfBLvZwUyersknTP7L-VAAL6rhddMmp_r
[website.fr-11 (out) 2013-11-09T15:40:26] 534-5.7.14 A_5pRpA> Please log in via your web browser and then try again.
[website.fr-11 (out) 2013-11-09T15:40:26] 534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
[website.fr-11 (out) 2013-11-09T15:40:26] 534 5.7.14 54 fr4sm15630311wib.0 - gsmtp]
[website.fr-11 (out) 2013-11-09T15:40:26] name: 'AuthError',
[website.fr-11 (out) 2013-11-09T15:40:26] data: '534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvlX\r\n534-5.7.14 V-dFQLgb7aRCYApxlOBuha5ESrQEbRXK0iVtOgBoYeARpm3cLZuUS_86kK7yPis7in3dGC\r\n534-5.7.14 N1sqhr3D2IYxHAN3m7QLJGukwPSZVGyhz4nHUXv_ldo9QfqRydPhSvFp9lnev3YQryM5TX\r\n534-5.7.14 XL1LZuJL7zCT5dywMVQyWqqg9_TCwbLonJnpezfBLvZwUyersknTP7L-VAAL6rhddMmp_r\r\n534-5.7.14 A_5pRpA> Please log in via your web browser and then try again.\r\n534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787\r\n534 5.7.14 54 fr4sm15630311wib.0 - gsmtp',
[website.fr-11 (out) 2013-11-09T15:40:26] stage: 'auth' }
My controller :
exports.contact = function(req, res){
var name = req.body.name;
var from = req.body.from;
var message = req.body.message;
var to = '*******#gmail.com';
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "******#gmail.com",
pass: "*****"
}
});
var mailOptions = {
from: from,
to: to,
subject: name+' | new message !',
text: message
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
res.redirect('/');
}
});
}
I solved this by going to the following url (while connected to google with the account I want to send mail from):
https://www.google.com/settings/security/lesssecureapps
There I enabled less secure apps.
Done
See nodemailer's official guide to connecting Gmail:
https://community.nodemailer.com/using-gmail/
-
It works for me after doing this:
Enable less secure apps - https://www.google.com/settings/security/lesssecureapps
Disable Captcha temporarily so you can connect the new device/server - https://accounts.google.com/b/0/displayunlockcaptcha
Easy Solution:
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: 'somerealemail#gmail.com',
pass: 'realpasswordforaboveaccount'
}
}));
var mailOptions = {
from: 'somerealemail#gmail.com',
to: 'friendsgmailacc#gmail.com',
subject: 'Sending Email using Node.js[nodemailer]',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Step 1:
go here https://myaccount.google.com/lesssecureapps and enable for less secure apps. If this does not work then
Step 2
go here https://accounts.google.com/DisplayUnlockCaptcha and enable/continue and then try.
for me step 1 alone didn't work so i had to go to step 2.
i also tried removing the nodemailer-smtp-transport package and to my surprise it works. but then when i restarted my system it gave me same error, so i had to go and turn on the less secure app (i disabled it after my work).
then for fun i just tried it with off(less secure app) and vola it worked again!
You should use an XOAuth2 token to connect to Gmail. No worries, Nodemailer already knows about that:
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
XOAuth2: {
user: smtpConfig.user,
clientId: smtpConfig.client_id,
clientSecret: smtpConfig.client_secret,
refreshToken: smtpConfig.refresh_token,
accessToken: smtpConfig.access_token,
timeout: smtpConfig.access_timeout - Date.now()
}
}
};
You'll need to go to the Google Cloud Console to register your app. Then you need to retrieve access tokens for the accounts you wish to use. You can use passportjs for that.
Here's how it looks in my code:
var passport = require('passport'),
GoogleStrategy = require('./google_oauth2'),
config = require('../config');
passport.use('google-imap', new GoogleStrategy({
clientID: config('google.api.client_id'),
clientSecret: config('google.api.client_secret')
}, function (accessToken, refreshToken, profile, done) {
console.log(accessToken, refreshToken, profile);
done(null, {
access_token: accessToken,
refresh_token: refreshToken,
profile: profile
});
}));
exports.mount = function (app) {
app.get('/add-imap/:address?', function (req, res, next) {
passport.authorize('google-imap', {
scope: [
'https://mail.google.com/',
'https://www.googleapis.com/auth/userinfo.email'
],
callbackURL: config('web.vhost') + '/add-imap',
accessType: 'offline',
approvalPrompt: 'force',
loginHint: req.params.address
})(req, res, function () {
res.send(req.user);
});
});
};
Worked fine:
1- install nodemailer, package if not installed
(type in cmd) : npm install nodemailer
2- go to https://myaccount.google.com/lesssecureapps and turn on Allow less secure apps.
3- write code:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'trueUsername#gmail.com',
pass: 'truePassword'
}
});
const mailOptions = {
from: 'any#any.com', // sender address
to: 'true#true.com', // list of receivers
subject: 'test mail', // Subject line
html: '<h1>this is a test mail.</h1>'// plain text body
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
})
4- enjoy!
I had the same problem. Allowing "less secure apps" in my Google security settings made it work!
Non of the above solutions worked for me. I used the code that exists in the documentation of NodeMailer. It looks like this:
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: 'user#example.com',
serviceClient: '113600000000000000000',
privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
expires: 1484314697598
}
});
Same problem happened to me too. I tested my system on localhost then deployed to the server (which is located at different country) then when I try the system on production server I saw this error. I tried these to fix it:
https://www.google.com/settings/security/lesssecureapps Enabled it but it was not my solution
https://g.co/allowaccess I allowed access from outside for a limited time and this solved my problem.
I found the simplest method, described in this article mentioned in Greg T's answer, was to create an App Password which is available after turning on 2FA for the account.
myaccount.google.com > Sign-in & security > Signing in to Google > App Passwords
This gives you an alternative password for the account, then you just configure nodemailer as a normal SMTP service.
var smtpTransport = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: "username#gmail.com",
pass: "app password"
}
});
While Google recommend Oauth2 as the best option, this method is easy and hasn't been mentioned in this question yet.
Extra tip: I also found you can add your app name to the "from" address and GMail does not replace it with just the account email like it does if you try to use another address. ie.
from: 'My Pro App Name <username#gmail.com>'
It is resolved using nodemailer-smtp-transport module inside createTransport.
var smtpTransport = require('nodemailer-smtp-transport');
var transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: '*******#gmail.com',
pass: '*****password'
}
}));
Many answers advice to allow less secure apps which is honestly not a clean solution.
Instead you should generate an app password dedicated to this use:
Log in to your Google account
Go to security
Under Signing in to Google enable 2-Step Verification
Under Signing in to Google click on App passwords.
You'll now generate a new password. Select the app as Mail and the device as Other (Custom name) and name it.
Save the app password
You can now use this app password instead of your log in password.
Try disabling captchas in your gmail account; probably being triggered based on IP address of requestor.
See: How to use GMail as a free SMTP server and overcome captcha
For me is working this way, using port and security (I had issues to send emails from gmail using PHP without security settings)
I hope will help someone.
var sendEmail = function(somedata){
var smtpConfig = {
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL,
// you can try with TLS, but port is then 587
auth: {
user: '***#gmail.com', // Your email id
pass: '****' // Your password
}
};
var transporter = nodemailer.createTransport(smtpConfig);
// replace hardcoded options with data passed (somedata)
var mailOptions = {
from: 'xxxx#gmail.com', // sender address
to: 'yyyy#gmail.com', // list of receivers
subject: 'Test email', // Subject line
text: 'this is some text', //, // plaintext body
html: '<b>Hello world ✔</b>' // You can choose to send an HTML body instead
}
transporter.sendMail(mailOptions, function(error, info){
if(error){
return false;
}else{
console.log('Message sent: ' + info.response);
return true;
};
});
}
exports.contact = function(req, res){
// call sendEmail function and do something with it
sendEmail(somedata);
}
all the config are listed here (including examples)
If you use Express, express-mailerwrapsnodemailervery nicely and is very easy to use:
//# config/mailer.js
module.exports = function(app) {
if (!app.mailer) {
var mailer = require('express-mailer');
console.log('[MAIL] Mailer using user ' + app.config.mail.auth.user);
return mailer.extend(app, {
from: app.config.mail.auth.user,
host: 'smtp.gmail.com',
secureConnection: true,
port: 465,
transportMethod: 'SMTP',
auth: {
user: app.config.mail.auth.user,
pass: app.config.mail.auth.pass
}
});
}
};
//# some.js
require('./config/mailer.js)(app);
app.mailer.send("path/to/express/views/some_view", {
to: ctx.email,
subject: ctx.subject,
context: ctx
}, function(err) {
if (err) {
console.error("[MAIL] Email failed", err);
return;
}
console.log("[MAIL] Email sent");
});
//#some_view.ejs
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><%= subject %></title>
</head>
<body>
...
</body>
</html>
For some reason, just allowing less secure app config did not work for me even the captcha thing. I had to do another step which is enabling IMAP config:
From google's help page: https://support.google.com/mail/answer/7126229?p=WebLoginRequired&visit_id=1-636691283281086184-1917832285&rd=3#cantsignin
In the top right, click Settings Settings.
Click Settings.
Click the Forwarding and POP/IMAP tab.
In the "IMAP Access" section, select
Enable IMAP.
Click Save Changes.
all your code is okay only the things left is just go to the link https://myaccount.google.com/security
and keep scroll down and you will found Allow less secure apps: ON and keep ON, you will find no error.
Just add "host" it will work .
host: 'smtp.gmail.com'
Then enable "lesssecureapps" by clicking bellow link
https://myaccount.google.com/lesssecureapps
Google has disabled the Less Secure App Access, Below is New Process to use Gmail in Nodejs
Now you have to enable 2 Step Verification in Google (How to Enable 2 Step Auth)
You need to generate App Specific Password. Goto Google My Account > Security
Click on App Password > Select Other and you will get App Password
You can use normal smtp with email and App password.
exports.mailSend = (res, fileName, object1, object2, to, subject, callback)=> {
var smtpTransport = nodemailer.createTransport('SMTP',{ //smtpTransport
host: 'hostname,
port: 1234,
secureConnection: false,
// tls: {
// ciphers:'SSLv3'
// },
auth: {
user: 'username',
pass: 'password'
}
});
res.render(fileName, {
info1: object1,
info2: object2
}, function (err, HTML) {
smtpTransport.sendMail({
from: "mail#from.com",
to: to,
subject: subject,
html: HTML
}
, function (err, responseStatus) {
if(responseStatus)
console.log("checking dta", responseStatus.message);
callback(err, responseStatus)
});
});
}
You must add secureConnection type in you code.
I was using an old version of nodemailer 0.4.1 and had this issue. I updated to 0.5.15 and everything is working fine now.
Edited package.json to reflect changes then
npm install
Just attend those:
1- Gmail authentication for allow low level emails does not accept before you restart your client browser
2- If you want to send email with nodemailer and you wouldnt like to use xouath2 protocol there you should write as secureconnection:false like below
const routes = require('express').Router();
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
routes.get('/test', (req, res) => {
res.status(200).json({ message: 'test!' });
});
routes.post('/Email', (req, res) =>{
var smtpTransport = nodemailer.createTransport({
host: "smtp.gmail.com",
secureConnection: false,
port: 587,
requiresAuth: true,
domains: ["gmail.com", "googlemail.com"],
auth: {
user: "your gmail account",
pass: "your password*"
}
});
var mailOptions = {
from: 'from#gmail.com',
to:'to#gmail.com',
subject: req.body.subject,
//text: req.body.content,
html: '<p>'+req.body.content+' </p>'
};
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log('Error while sending mail: ' + error);
} else {
console.log('Message sent: %s', info.messageId);
}
smtpTransport.close();
});
})
module.exports = routes;
first install nodemailer
npm install nodemailer --save
import in to js file
const nodemailer = require("nodemailer");
const smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "example#gmail.com",
pass: "password"
},
tls: {
rejectUnauthorized: false
}
});
const mailOptions = {
from: "example#gmail.com",
to: sending#gmail.com,
subject: "Welcome to ",
text: 'hai send from me'.
};
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log(error);
}
else {
console.log("mail sent");
}
});
working in my application
You may need to "Allow Less Secure Apps" in your Gmail account (it's all the way at the bottom). You also may need to "Allow access to your Google account".
You also may need to "Allow access to your Google account".
This is my Nodemailer configuration which worked after some research.
Step 1: Enable lesssecureapp
https://www.google.com/settings/security/lesssecureapps
Step 2: The Nodemailer configuration for Gmail
Setting up the transporter : A transporter is going to be an object that can send mail. It is the transport configuration object, connection URL, or a transport
plugin instance
let transporter = nodemailer.createTransport({
service: 'gmail', // the service used
auth: {
user: process.env.EMAIL_FROM, // authentication details of sender, here the details are coming from .env file
pass: process.env.EMAIL_FROM_PASSWORD,
},
});
Writing the message
const message = {
from: 'myemail#gmail.com', // sender email address
to: "receiver#example.com, receiver2#gmail.com", // reciever email address
subject: `The subject goes here`,
html: `The body of the email goes here in HTML`,
attachments: [
{
filename: `${name}.pdf`,
path: path.join(__dirname, `../../src/assets/books/${name}.pdf`),
contentType: 'application/pdf',
},
],
Sending the mail
transporter.sendMail(message, function (err, info) {
if (err) { // if error
console.log(err);
} else {
console.log(info); // if success
}
});
I also had issues with nodemailer email sending when running on Vercel lambda in production.
What fixed it in my case was to await for sendMail Promise to resolve.
I also added nodemailer-smtp-transport like suggested in this thread but I don't think it made a difference.
Here is my whole function:
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: '***#gmail.com',
pass: process.env.SMTP_PASSWORD,
},
}));
async function contact(req: any, res: any) {
try {
const response = await transporter.sendMail({
from: '"*** <***gmail.com>', // sender address
to: "***#gmail.com", // list of receivers
subject: `***`, // Subject line
html: `${req.body.message}<br/><br/>${req.body.firstname} ${req.body.lastname} - <b>${req.body.email}</b>`, // html body
});
} catch (error: any) {
console.log(error);
return res.status(error.statusCode || 500).json({ error: error.message });
}
return res.status(200).json({ error: "" });
}
export default contact;
As pointed out by Yaach, as of May 30th, 2022, Google no longer supports Less Secure Apps, and instead switched over to their own Gmail API.
Here is the sample code for Gmail SMTP with nodemailer.
"use strict";
const nodemailer = require("nodemailer");
async function main() {
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
transportMethod: "SMTP",
secureConnection: true,
port: 465,
secure: true, // upgrade later with STARTTLS
auth: {
user: "yourEmail#gmail.com",
pass: "Your App Specific password",
},
});
let info = await transporter.sendMail(
{
from: "yourEmail#gmail.com",
to: "to#gmail.com",
subject: "Testing Message Message",
text: "I hope this message gets delivered!",
html: "<b>Hello world?</b>", // html body
},
(err, info) => {
if (err) {
console.log(err);
} else {
console.log(info.envelope);
console.log(info.messageId);
}
}
);
}
main();
Less secure option is not supported anymore by gmail.
For sending email from third party, gmail is also not allowing with its user password.
You should now use App Password to resolve this issue.
Hope this link will help to set your app password.
https://support.google.com/mail/answer/185833?hl=en
There is another option to use SendGrid for email delivery with no failure. A lot of the time, Nodemailer gives failure for mail which could happen frequently.
Nodemailer can be found in the link.

Categories

Resources