I've got a basic email setup done for sending an email using Nodemailer with AngularJS and NodeJS and I've got the project deployed on heroku.
The emailing seems to be working just fine when I am running the app on heroku, but when I get it deployed to Heroku no emails are sent.
For authentication, I am using a Gmail address and I also have a bcc
to another Gmail address. So from
and bcc
addresses are two different Gmail addresses. The from
address is the same as the address used for authentication.
Could somebody help me with resolving this issue?
Edit: Adding code
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'foobar'
}
});
router.post('/send',function(req,res){
var mailOptions = {
from: 'Foo Bar ✔ <[email protected]>',
to: req.body.email,
subject: "Hello " + req.body.email,
text: 'Hello ' + req.body.email + '✔',
html: "<p>Hello " + req.body.email + " </p>",
bcc: "[email protected]"
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
res.send(200);
}
});
});
As far as I have tested, Nodemailer works fine on Heroku. If Nodemailer does not return an error, then the MDA server accepted the e-mail for delivery. You can check the exact response from the MDA (assuming you are using the SMTP transport) from the response object.
To get Gmail working with nodemailer, most times, all you have to do is configure your google account by allowing access to "less secure apps" from the security. With this enabled, you could use your Google email address and password for nodemailer and start sending emails right away.
You can indeed use external SMTP providers to send email through Heroku (I do this on several of the apps I run).
SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.
I believe this is an issue with google account security.
A few step to verify this:
Start your server locally and sends the email.
Check your account alerts for unknown sign-in.
This can be temporally resolved by: https://accounts.google.com/DisplayUnlockCaptcha
A more permanent resolution would be to change your password to a stronger level:
upper case letter + lower case letter + special symbols + numbers
Instead of using direct gmail credentials like this
auth: {
user: '[email protected]',
pass: 'foobar'
}
Use OAuth2
auth: {
type: 'OAuth2',
user: '[email protected]',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}
Google blocks the heroku IPs (unsafe), if we are using direct credentials like you mentioned above. You can refer this Medium article here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With