Nodemailer not working on Amazon EC2 instance - node.js

I am EC2 instance up and running , I wanted to integrate nodemailer in my application. Following the https://www.npmjs.com/package/nodemailer I was able to send email from my localhost. When the same code I integrated on EC2 instance I am getting Invalid login error . Sometime gmail blocks login from other application and send confirm mail to the inbox. I didnt get any such mail also. Do I need to enable some port on EC2 instance or I can use nodemailer at all on EC2 instance. Please suggest

Gmail is not a production SMTP service. Configure nodemailer to send mail from a production mailer, such as AWS Simple Email Service. Like gmail, SES is a "well-known service" in nodemailer. There's a great example of using SES in the nodemailer README.

No need to enable aws ses service
Open port : 465 on aws(e2c) instance in outbound section.
Test if port really opened : with nmap command nmap 14.247.74.33.
Note : do not proceed until you see open ports like
Starting Nmap 5.21 ( http://nmap.org ) at 2012-08-15 19:01 IST
Nmap scan report for 14.247.74.33
Host is up (0.058s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
23/tcp open telnet
80/tcp open http
465 open
Simple nodemailer configuration works
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: EMAIL_ID,
pass: EMAIL_PASSOWRD,
},
});
Full example code

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 use specific outboung port

I'm having issues sending email from nodemailer due to the company firewall.
I'm using the default configuration of nodemailer but I get a timeout error, and seems that the problem is due to the server/firewall is configured only allow outbound traffic to go through port 587.
Is there any way to configure nodemailer to use this specific outgoing port and not a random one?
The destination port defaults to 587. If secure: true is set, 465.
nodemailer.createTransport({ host: 'mx', port: 465, secure: true })
TCP source ports are chosen randomly by the host, but that's nothing to do with nodemailer.

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

Openssl issue when sending email through AWS SES

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

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