Openssl issue when sending email through AWS SES - node.js

Update:
If I follow the instructions from Using the Command Line to Send Email Using the Amazon SES SMTP Interface, I can get the email to send perfectly from my local and my ec2 instance.
We're using nodemailer to send email through SMTP. When we configure everything using Gmail's SMTP user/pass, everything works fine.
We're trying to move to AWS SES. Everything is seemingly set up fine (domains are verified, we're out of SANDBOX mode, and we're using the SMTP user/pass credentials).
We're using the exact same code, and just swapping out the smtp user/pass/host in our credentials file. When sending the mail with the SES credentials, we're getting this error:
Email was not send due to the following error: [Error: 62024:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:
] {
library: 'SSL routines',
function: 'ssl3_get_record',
reason: 'wrong version number',
code: 'ESOCKET',
command: 'CONN'
}
According to this GitHub issue, the problem seems to be:
You are either trying to use TLS on a non-TLS port or the openssl
version you use is not compatible with the server.
I'm not quite sure what to do with that information. Our SSL cert is on ELB.
Here's the code that's responsible for sending the actual email:
"use strict";
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE,
auth: {
user: process.env.SMTP_AUTH_USER,
pass: process.env.SMTP_AUTH_PASS
}
});
module.exports = {
sendMail: (to, subject, html, callback) => {
const mailOptions = {
from: "no-reply#xyz.com",
to,
subject,
html
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return callback(err);
}
return callback(null, info);
});
}
};

TLDR;
Use port 465 when the secure option is true.
What I Did
I went by the comment of #moulder on the question and it worked.
To be clear, you should use 465, true to use SSL to connect, or 587, false to connect without SSL and upgrade via STARTTLS. Any other combination won't work. The code was buggy, fixing it here:
Source: Fabien Potencier at symfony/symfony#34846
See also symfony/symfony/34067
What Amazon Says
Just like there are HTTP and HTTPS ("s" for secure), there is SMTP and SMTPS (kinda)... As for the secure version of the communication, there are to ways to establish that security.
STARTTLS - The client connects with no security. The server says it supports security. Then, the client negotiates security contracts with the SMTP server and migrate from insecure to secure communication.
TLS Wrapper - The client goes secure from the beginning.
Source: Amazon SES Docs - Connecting to an SMTP endpoint

Related

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

nodemailer SMTP server connection issue (Error: connect ETIMEDOUT) but it works for another system not in mine

Iam trying to send node mailer using webmail instead of GMail using smtp sever as follows..
// config connections
"nodemailer" : {
"host": "smtp.in-sciencelabs.com",
"port": 587,
"auth": {
"user": "myuser.com",
"pass": "myp#ssword"
}
// in nodemailer.js code iam importing the config details....
const transporter=
nodemailer.createTransport(smtpTransport((config["nodemailer"])));
var mailOptions = {
from: `${config["nodemailer"]["user"]}`,
to: tomail,
subject: subject ,
text: data
};
transporter.verify(function(error, success) {
if (error) {
console.log(error,"17");
} else {
console.log("Server is ready to take our messages");
}
});
i tried to verify the connections so its shows an error like this
(Error: connect ETIMEDOUT)***.**.***.***:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ECONNECTION',
syscall: 'connect',
address: 'xxx.xx.xxx.xxx',
port: 587,
command: 'CONN'
}
here iam trying this but its not workout for me but in another system emails are sending so i dont know which setting is going to change in system for sending an email could you please help me out from this problem and i also changed the node version to 12.18.3
Have you looked at this answer.
It turns out that in order for Google to authorize a third party server to access your account via SMTP now, you have to enable “Less Secure Apps” on your gmail account, if you want to use username/password (more info here).
So you have two option:
use OAuth
make your account less secure
you also need to check your server firewall setting,
This may be firewall problem, Problem can be solved by enabling TCP inbound and outbound in firewall settings on your server, if you are using digital ocean then you can find it in firewall settings in panel

error 550 Domain not allowed in header from

I am trying to get namecheap c-panel email hooked up with my nodemailer server instance but I am unable to find how to do that.
here is my nodemailer code
let transporter = nodemailer.createTransport({
host: "premium174.web-hosting.com",
port: 465,
secure: true,
auth: {
user: username,
pass: password
},
connectionTimeout: 30000
});
from part in the sendMail method contains the email I am logging in to
I am able to establish a connection the server using these details but I can't send any email, AT ALL. anytime I try to send it the error I get is
code: 'EENVELOPE',
response: '550-"Your IP: my-ip-here : Your domain domain-here is not allowed in header\n' +
'550 From"',
responseCode: 550,
command: 'RCPT TO'
the send mail part is all correct and it works with the other emails I have but this one just would not work
You can visit Email Routing from cPanel and choose "Local Mail Exchanger" under Configure Email Routing.
Ok so what I had to do was to change namecheap email routing to local and it worked :D

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

Accessing SMTP server with AUTH NTLM from Node.js

I'm trying to access a SMTP server with AUTH type of NTLM.
I'm using nodemailer and nodemailer-smtp-transport as such:
var config = require('./config.json');
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
host : config.mailer.host,
port: config.mailer.port,
auth: {
user: config.mailer.username,
pass: config.mailer.password
},
authMethod: 'PLAIN'
}));
But it doesn't work. The error I get is:
{ [Error: Invalid login: 504 5.7.4 Unrecognized authentication type]
code: 'EAUTH',
response: '504 5.7.4 Unrecognized authentication type',
responseCode: 504 }
Which makes sense, because if I telnet into the SMTP server
ehlo server.domain.net
250-server.domin.net Hello [10.100.10.100]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
And enter
AUTH PLAIN
I get
504 5.7.4 Unrecognized authentication type
But inside Node, if I change the authMethod to 'NTLM', I get an error that says
{ [Error: Unknown authentication method "NTLM"] code: 'EAUTH' }
I'm suspecting that nodemailer just doesn't support NTLM. If that's the case, how do I connect to a SMTP server that requires NTLM authentication type?
Thanks
My company ran into the same problem a few days ago. The options we considered were:
Ask the exchange server admins to enable PLAIN auth under STARTTLS (it is secure and appears to only involve ticking a couple of checkboxes)
Set up a local relay (e.g. postfix) that relays to Exchange, and use the postfix relay from nodemailer
Fork nodemailer and add NTLM support
Unfortunately we hit political issues on the easy options (1) and (2), so had to fork nodemailer.
I didn't send a pull request yet, but the fork is here. For the time being the easiest way to use it is via npm by referring directly to the github project in your package json, e.g.:
"dependences": {
"nodemailer": "steveliles/nodemailer"
}
If you're interested, most of the change was actually in a sub-sub-project (smtp-connection), and the forks of nodemailer, nodemailer-smtp-pool, and nodemailer-smtp-transport are only necessary to get my smtp-connection fork to be picked up.
We didn't need to implement the NTLM protocol, as SamDecrock's httpntlm already did the hard work.
It has only been tested against Exchange 2007 over TLS (with STARTTLS) and no domain or workstation.
If you do need domain + workstation in the credentials, just add them to nodemailer's options.auth and they will be passed through, e.g.
var smtpConfig = {
host: 'ntlm.boo.hoo',
port: 25,
auth: {
domain: 'windows-domain',
workstation: 'windows-workstation',
user: 'user#somedomain.com',
pass: 'pass'
}
};
We were even more unlucky in that the exchange server we're connecting to doesn't have a valid SSL certificate, but luckily nodemailer can handle that by setting tls: {rejectUnauthorized: false} in the options.
From version 6.x.x, you can use custom auth:
https://github.com/nodemailer/nodemailer-ntlm-auth
Refs: https://nodemailer.com/smtp/#authentication
If this is an internal/service type application and your server admin doesn't mind, you can ask them to create a host without authorization and just get rid of
auth: {
user: '-----------',
pass: '-----------'
}
Since I'm just creating a service type app just to send emails on a schedule, my server admin allowed this for me.
Worked for me but I'm sure this solution is not for everyone!

Resources