I've set up a basic NodeJS server (using the nodemailer module) locally (http://localhost:8080
) just so that I can test whether the server can actually send out emails.
If I understand the SMTP option correctly (please correct me if I'm wrong), I can either try to send out an email from my server to someone's email account directly, or I can send the email, still using Node.js, but via an actual email account (in this case my personal Gmail account), i.e using SMTP. This option requires me to login into that acount remotely via NodeJS.
So in the server below I'm actually trying to use NodeJs to send an email from my personal email account to my personal email account.
Here's my simple server :
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport("SMTP", {
service: 'Gmail',
auth: {
user: '*my personal Gmail address*',
pass: '*my personal Gmail password*'
}
});
var http = require('http');
var httpServer = http.createServer(function (request, response)
{
transporter.sendMail({
from: '*my personal Gmail address*',
to: '*my personal Gmail address*',
subject: 'hello world!',
text: 'hello world!'
});
}).listen(8080);
However, it's not working. I got an email by Google saying :
Google Account: sign-in attempt blocked If this was you You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
I couldn't find a solution for the above problem on the nodemailer GitHub page. Does anyone have a solution/suggestion ?
Thanks! :-)
While the above answers do work, I'd like to point out that you can decrease security from Gmail by the following TWO steps.
STEP #1
STEP #2
i just set my domain to: smtp.gmail.com and it works. I am using a VPS Vultr.
the code:
my ejs template (e-mail.ejs):
Make sure:
have a nice day ;)
And install smtp module as dependency:
npm install smtp
Go to https://myaccount.google.com/lesssecureapps and change it ON because Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.
The answer is in the message from google.
Go to : https://www.google.com/settings/security/lesssecureapps
set the Access for less secure apps setting to Enable
For the second part of the problem, and in response to
I will refer you to the nodemailer github page, and this piece of code :
It differs slightly from your code, in the fact that you have :
nodemailer.createTransport("SMTP"
. Remove the SMTP parameter and it works (just tested). Also, why encapsulating it in a http server? the following works :For debugging purpose it is handy to implement a callback function (they never do on the nodemailer github page) which shows the error message (if there is one).
It helped me solve my problem... Turns out newer versions are not working properly:
"Looks like nodemailer 1.0 has breaking changes so 0.7 must be used instead: http://www.nodemailer.com/
Message posted on nodemailer as of 12/17/15:
Do not upgrade Nodemailer from 0.7 or lower to 1.0 as there are breaking changes. You can continue to use the 0.7 branch as long as you like. See the documentation for 0.7 here."
I found this answer here
For those who actually want to use OAuth2 / don't want to make the app "less secure", you can achieve this by
SCOPES
var from['https://www.googleapis.com/auth/gmail.readonly']
to['https://mail.google.com/']
in the quickstart js file provided as suggested in troubleshooting at https://nodemailer.com/smtp/oauth2/acessToken
,refreshToken
, andexpires
attributes needed in the OAuth2 Examples for NodemailerThis way you can use OAuth2 authentication like the following
instead of storing your gmail password in plaintext and downgrading the security on your account.