Authentication errors when sending intermittent emails from gmail using Heroku / node.js / emailjs - node.js

I am running a scraper app on Heroku that sends emails every once in a while. Sometimes it'll be every 2 minutes, sometimes it won't send anything for 2 days-depends on what the scraper finds. I am running into continual errors with email authentication. I have tried configuring the email with nodemailer and emailjs. Both run into the same issue. I am wondering if it is because of the way Heroku servers are distributed?
I have taken the following steps:
1) unlocked the account at: https://accounts.google.com/DisplayUnlockCaptcha
2) allowed less secure apps here: https://myaccount.google.com/lesssecureapps
With both nodemailer and emailjs, I notice that it works fine for a couple hours but then if no emails are sent for a day, as soon as it tries to send another one I receive an authentication error. Here is the code using emailjs that works for a couple hours then fails with an authentication error:
var server = email.server.connect({
user: process.env.CRAWLER_MAIL,
password:process.env.CRAWLER_PWD,
host: "smtp.gmail.com",
ssl: true
});
var message = {
text: "Placeholder",
from: 'NAME <'+ process.env.CRAWLER_MAIL+'>',
to: eachEmail[0],
subject: emailSubject,
attachment:
[
{data:emailBody, alternative:true}
]
};
server.send(message, function(err, message) { console.log(err || message); });
And here is the error from the Heroku log:
Error: authorization.failed (bad response on command '[redacted]': -5.7.14 https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=[redacted]-)
I don't use 2-step verification for this account, so I don't think I need an app password. Any other ideas about how I can get this to consistently work?

Related

No logs uploaded to datastream on node.js with winston

I am trying to send messages to our elasticsearch endpoint, if I'm not using datastreams it works fine but when I am it stops working.
I have added the datastream=true option to logger constructor.
I just get no messages
I am not getting any errors or any logs on the console, just silently fails.
The logger init code
basicTransports.push(
new ElasticsearchTransport({
dataStream: true,
indexPrefix: indexPrefix,
indexSuffixPattern: 'YYYY.MM.DD',
client: new Client({
node: node,
auth: {
username: username,
password: password
},
})
})
)
an example log message is:
Logger.getInstance().info('connected successfully');
EDIT: I used wireshark to follow requests hoping I can point an error that is not reported by the client, but I actually couldn't find any messages going out of there... To be fair there are a lot of elasticsearch communications because we have a few services also talking at the same time, but a search for the index yielded nothing...

Nodemailer: My frontend website can't send emails through node mailer from a different IP address

My frontend portfolio has an emailing option which sends a post req to nodemailer in the backend.
My portfolio is already deployed in firebase and I'm currently running the backend on the localhost server.
I face no issues when I send emails on my laptop, same IP address, while I receive an error whenever I try sending emails on a different device.
I have searched around for a day now and I can't find an available solution. Any idea on how I can solve this issue?
My nodemailer configuration:
const transporter = nodemailer.createTransport(smtpTransport({
name: 'domainname',
host: 'domainname.com',
port: 443,
secure: true,
service: 'gmail',
auth: {
user: '*****#gmail.com',
pass: '******' //Google's less secure apps option enabled.
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
}
}))
The following error is logged into the console when sending email fails:
POST http://localhost:4000/api/email/ net::ERR_CONNECTION_REFUSED
TypeError: Failed to fetch
at main.1ec56b92.js:2:347746
at c (main.1ec56b92.js:2:168378)
at Generator._invoke (main.1ec56b92.js:2:168166)
at Generator.next (main.1ec56b92.js:2:168807)
at Ft (main.1ec56b92.js:2:332714)
at i (main.1ec56b92.js:2:347506)
at main.1ec56b92.js:2:347567
at new Promise (<anonymous>)
at main.1ec56b92.js:2:347446
at main.1ec56b92.js:2:348157
Thank you
As Derpirscher pointed out, the host of the backend was hardcoded as localhost:4000 hence why it worked on my laptop and failed on my phone.
The solution for me was to deploy the code to a server -like Heroku- and use its address to connect to the nodemailer API, eg: https://project.herokuapp.com/api/email instead of https://localhost:4000/api/email

SMTP server using hmailserver. Host cannot be resolved to a type

I am trying to create a local SMTP server. I configured everything according to this: Link to set-up hmailserver
After that, I ran my diagnostics thing and it said host(in my case, noreply#bufferoverflow.com) cannot be resolved. I googled and read the documentation and question section of hmailserver Docs and found that I am missing a DNS configuration on my machine.
I followed this article to set-up the DNS ip to name: How to add ip domain mapping in windows
127.0.0.1 bufferoverflow.com
on my
C:\Windows\System32\drivers\etc>notepad hosts.ics
It still said host cannot be resolved to a type. I tried to see if the port was actually open and listening. So I did this command in CMD:
netstat -a
It's listening. I doubled checked everything but coudn't found where I was going wrong. I created a simple nodejs server and hosted it on port 80 at localhost and typed "bufferoverflow.com" in my browser it opened 127.0.0.1 aka localhost which is because it has an ip domain mapping I have given.
Now I am lost as why the hostname can't be resolved. My DNS is cool. Everthing is configured as docs says and the port is also open and listening. Please help me.
So based on what you said you created your own SMTP server which is nice and all to learn, but I would recommend using SendinBlue 3rd party service FREE and gives you 300 mails/day
Getting Started with sendinblue:
Link: https://www.sendinblue.com/
Go make and an account free of charge till you decide to pay you'll read there
Once your account is all set, and activated often SMTP service for ones account is not automatically activate so you'd have to email them and they literally respond within 24hr I LOVE THE SERVICE(I am not promoting nor sponsored to hype them or anything just saying the truth of what I have experienced).
So now at this point your account and SMTP service is activated, now question is how do you use sendinBlue?
In your dashboard on your Top right you will see your username
Click on that and a dropdown menu should appear
Then you want to go to SMTP & API
Once you then you will see a menu with API Keys on the left and SMTP on the right
You want to click on the SMTP.
Once you the you will see Login that would be the email you registered with and you will see Master password now that is what you use to authenticate yourself.
Implementation: Now you have everything sorted you know where you keys are and login
In .env file
sendinBlue__login=<Your email that your saw in that smtp tab>
sendinBlue__key= <Key you saw in that smtp tab>
In your file where you want to send email
const nodemailer = require('nodemailer');
router.post('/api/email', (request, response)=>{
const transporter = nodemailer.createTransport({
service:'SendinBlue',
auth:{
user:process.env.sendinBlue__login,
pass:process.env.sendinBlue__key
}
})
const mailOptions = {
from:process.env.sendinBlue__login
to:'example#gmail.com',
subject:'Order confirmation',
html:`
<h1>Confirmation of your received order</h1>
`
}
transporter.sendMail(mailOptions, (error, info)=>{
if (error){
console.log(error);
return response.status(500).json({msg:"Email failed to send probably network problems or SMTP not activated"});
}
return reponse.status(200).json({msg:'Email sent'});
})
})
Now this is how easy sendinBlue works just simple signup and account activation also SMTP activation if not activated from get go by email customer support
If that guide above did not make sense to you: Shameless plug
You can go checkout my video that I did and I was using sendinBlue with nodemailer for emailing it's a bit at the end
Link: https://youtu.be/5vWXbGIdDQw
Now If you want to send an email using noreply#domain.com with sendinBlue you do not even have to have the email activated but still can send
How you go about this is the way you craft your mailOptions
router.get("/api/test", (request, response) => {
const transpoter = nodemailer.createTransport({
service: "SendinBlue",
auth: {
user: process.env.sendinBlue__email,
pass: process.env.sendinBlue__key,
},
});
const mailOptions = {
from: "noreply#Test.com",
to: "juniorWebProjects#gmail.com",
subject: "Confirmation Order",
html: `
<h1>Order Received</h1>
`,
};
transpoter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err);
}
return console.log("Message sent");
});
});
See on my mailOptions I explicitly wrote myself the email that I want and when sendinBlue sends that email the receiver will see that is coming from noreply#Test.com

Send mails with nodeJs

I am doing on my website a reset of password, and i would like to send an email to the user who is asking this reset. I looked few packages like nodemailer and sendmail. The problem with nodemailer, i don't want to give my email in the code, even if i'm the only who is seeing this. The problem with sendmail is:
error on connectMx Error: can not connect to any SMTP server
code sendmail :
const sendmail = require('sendmail')();
sendmail({
from: 'test#test.com',
to: 'adz#gmail.com',
subject: 'Hello',
html: 'test mail '
}, function (err, reply) {
console.log(err && err.stack)
console.dir(reply)
}),
When i was working only php, there was a function called "mail", it didn't need an account gmail, hotmail ... So i'm asking to you, is there a way to don't give my account to nodemailer or not please, or is there an other way to send mails with nodeJS ?
Thanks a lot and i hope i have been clear about my problem (sorry for the bad english).

sending emails in node.js / nodemailer

I tried to use nodemailer to send emails using my gmail account.
However, Google rejects my login as suspicious and thinks I am a hacker.
I tried Yahoo which does send the email.
My questions are:
1) How can I configure nodemailer to send emails thru gmail
2) Standard/Reliable email library in node.js community with good support that can be used in production.
I was recently playing around with this as well, and the error I received from Google was simply stating that I needed to create an application specific password. Try following the instructions here.
As for your second question, I do not know of any reasons as to why nodemailer can't be used in production.
I used emailjs, and haven't had that issue. Not sure if that's because it's email.js or because it's Google Apps vs Gmail, or maybe Google is just less suspicious with this app for some reason. Maybe useful to try to triangulate:
$> npm install emailjs
emailjs = require('emailjs');
...
var server = emailjs.server.connect({
user:"myname#mygoogleapp.com",
password:"Secret#!1",
host:"smtp.gmail.com",
ssl:true
});
server.send({
text: message
from:"Display name <return#mydomain.com>",
to:email,
subject:"Subject"
},
function (err, message) { ... }

Resources