Nodemailer works on local but not on production - gmail

I put my application online and tried my contact form but the nodemailer is not working due to a smtp connection timeout.
Here is my transport
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.MAIL_USERNAME, //
pass: process.env.MAIL_PASSWORD // Google app password
},
tls: {
rejectUnauthorized: process.env.APP_ENV == 'local' ? false : true // which is true but also tried false
}
});
and my sendmail function
let info = await transporter.sendMail({
from: `"${data.firstname} ${data.lastname}" <${data.email}>`,
to: process.env.MAIL_TO_DESTINATION, // list of receivers
subject: data.subject,
html: `<p><strong>Fullname:</strong> ${data.firstname} ${data.lastname}</p>
<p><strong>Email:</strong> ${data.email}</p>
<p><strong>Subject:</strong> ${data.subject}</p>
<p><strong>Message:</strong> ${data.message}</p>`
});
Error
(node:3743) UnhandledPromiseRejectionWarning: Error: Connection timeout
at SMTPConnection._formatError (/home/clients/974a427295deac388e59a945b2c70917/portfolio-mytdragon/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._onError (/home/clients/974a427295deac388e59a945b2c70917/portfolio-mytdragon/node_modules/nodemailer/lib/smtp-connection/index.js:770:20)
at Timeout._connectionTimeout.setTimeout (/home/clients/974a427295deac388e59a945b2c70917/portfolio-mytdragon/node_modules/nodemailer/lib/smtp-connection/index.js:235:22)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
I don't know what is wrong since it works locally. Anyone know why?
Thanks.

I found out how to solve it. The problem wasn't from my script but from the host. I had to open manually the 465 tcp exit port.

Related

Nodemailer error: Error: connect ECONNREFUSED 127.0.0.1:587 suddenly appears

So I´m currently updating my JS backend to TS.
The project was written back in 2018 or 2019 and had some outdated stuff going on.
But it actually worked all well.
Now, for some reason, nodemailer wont work anymore and throws this error:
Error: connect ECONNREFUSED 127.0.0.1:587
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) { errno: -61, code: 'ESOCKET', syscall: 'connect', address:
'127.0.0.1', port: 587, command: 'CONN' }
I know there have been posts on this topic, but I don´t think my configuration is wrong, because it worked before totally fine and I didn´t really change anything, except for adding some types.
// defining transport to connect to mail host
const transport = nodemailer.createTransport({
host: process.env.EMAIL_PROVIDER,
port: 587,
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false,
},
debug: true,
auth: {
user: process.env.EMAILADDRESS_SENDER,
pass: process.env.EMAILPASSWORD,
},
});
export const sendOrderCreatedEmail = async (customer: Customer, paymentId: string) => {
const mailOptions: MailOptions = {
from: process.env.EMAILADDRESS_SENDER!,
to: process.env.EMAILADDRESS_RECEIVER!,
subject: `new Audit ${paymentId}`,
html: ...,
};
await sendEmail(mailOptions);
};
const sendEmail = async (mailOptions: MailOptions) => {
// send email with defined transport
transport.sendMail(mailOptions).catch(err => {
console.log(err);
});
};
"nodemailer": "^6.7.5",
any idea what could be wrong there? Cheers!

Node.js nodemailer error - wrong version number/invalid greeting

I have a big problem with setting up the nodemailer on my node.js server. Tried everthing I found on the internet but nothing works. The only thing that was easy to setup was the gmail service. but unfortunately I cannot use that one.
With secure set to true, i get an ssl error with the reason wrong version code.
[Error: 22468: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'
}
But when I try to set secure to false, then I get an invalid greeting error.
Error: Invalid greeting. response=* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc. See COPYING for distribution information.: * OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc. See COPYING for distribution information.
at SMTPConnection._actionGreeting (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:1189:27)
at SMTPConnection._processResponse (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:932:20)
at SMTPConnection._onData (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:739:14)
at Socket.SMTPConnection._onSocketData (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:284:9)
at Socket.Readable.push (_stream_readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
code: 'EPROTOCOL',
response: '* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc. See COPYING for distribution information.',
command: 'CONN'
}
My code is the following:
const transporter = nodemailer.createTransport({
host: process.env.MAIL_HOST, // mx.example.com
port: process.env.MAIL_PORT, // 143
secure: true,
auth: {
user: process.env.MAIL_ADDRESS,
pass: process.env.MAIL_PWD
}
})
I checked the credentials a thousand time, they are definetly not the problem.
Hope anyone can help me.
Thanks in advance.
Refering to this issue mentioned here: https://github.com/andris9/Nodemailer/issues/165
See if this helps, adding the tls.ciphers option to use SSLv3:
const transport = nodemailer.createTransport({
host: process.env.MAIL_HOST, // mx.example.com
port: process.env.MAIL_PORT, // 143
secureConnection: false, // TLS requires secureConnection to be false
auth: {
user: process.env.MAIL_ADDRESS,
pass: process.env.MAIL_PWD
},
tls: {
ciphers:'SSLv3'
}
});
For Outlook365, this should work:
service: "Outlook365",
auth: {
user: '[YOUR_O365_EMAIL]',
pass: '[YOUR_O365_PASSWORD]'
},
Refer here: https://stackoverflow.com/a/37725123/9360885
If you're using HotMail, then remove host and port, and just add service: "hotmail".
use
secure: false, // true for 465, false for other ports
Example from nodemailer docs:
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
source: nodemailer examples
I used port 465 with Gmail and that worked. Got the "wrong version number" error with 587. Example below is for a GCP service account in a Cloud Function.
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: process.env.GMAIL_ADDRESS,
serviceClient: process.env.CLIENT_ID,
privateKey: process.env.PRIVATE_KEY.replace(/\\n/g, "\n"),
},
});
I also ran in this problem. But I managed to fix it.
It's quite simple to solve.
Solution:
port: process.env.MAIL_PORT * 1
This will change your process.env.MAIL_PORT type from a string to integer.
Why there was an error?
NodeMail require a port with a type of integer, but you were passing a string. So that caused an error.
If you are still receiving the issue, fix by inputting this into your browser manually while logged in.
https://accounts.google.com/DisplayUnlockCaptcha
change smtp port 587 to 465
smtp:{
name:"noreply#gmail.com",
host:process.env.SMTP_HOST,
secure: true,//true
port: 465,//465
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
}, // here it goes
},

Timeout error on using nodemailer with cpanel custom email

I have created a custom email in my cpanel dashboard, so I can be sending emails with my domain name. I was using google before with nodemailer it works out well, but now trying to do it with my domain host all i get is connection timeout, i don't really know where the problem is from, but my mail host is up and running.
this question may have similar duplicates but i have gone through them but no help..
my records for the host, I don't know if the TTL and record type is causing it
TTL: 14400
record-type : CNAME
var smtpTransport = nodemailer.createTransport({
host: 'mail.mydomain.com',
port: 465,
secure: true,
auth: {
user: 'me#mydomain.com',
pass: process.env.Password
}
});
var mailOptions = {
to: somebody#gmail.com,
from: 'me#mydomain.com',
subject: 'welcome',
html: '<p>Hello this is a notification </p>',
};
smtpTransport.sendMail(mailOptions, function(err, sent){
if(err){
console.log(err)
} else {
console.log('message sent')
}
});
{ Error: Connection timeout
at SMTPConnection._formatError (/home/ubuntu/workspace/bitcoin.1/main/node_modules/nodemailer/lib/smtp-connection/index.js:591:19)
at SMTPConnection._onError (/home/ubuntu/workspace/bitcoin.1/main/node_modules/nodemailer/lib/smtp-connection/index.js:564:20)
at Timeout._connectionTimeout.setTimeout (/home/ubuntu/workspace/bitcoin.1/main/node_modules/nodemailer/lib/smtp-connection/index.js:256:18)
at ontimeout (timers.js:386:11)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5) code: 'ETIMEDOUT', command: 'CONN' }
Any help is highly appreciated :)

Error 535 while setting up nodemailer with GoDaddy

I'm wondering if anyone has figured out how to use GoDaddy hosted email with nodemailer. I have an email submission box from my contact form, and it triggers the following script:
function sendEmail ( to, subject, body ) {
var transporter = nodemailer.createTransport({
service: 'Godaddy',
host: "relay-hosting.secureserver.net",
port: 465,
secureConnection: true,
auth: {
user: 'email#godaddydomain.com',
pass: 'password'
}
});
var mailOptions = {
from: 'email#godaddydomain.com',
to: to,
subject: subject,
text: body
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
I've tried a few different options, and made sure my ports are all open, but I'm having a hard time with the authentication. I keep getting errors like this:
{ Error: Invalid login: 535 Authentication Failed for email#godaddydomain.com. User does not have any relays assigned.
at SMTPConnection._formatError (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:577:19)
at SMTPConnection._actionAUTHComplete (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:1306:34)
at SMTPConnection._responseActions.push.str (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:349:26)
at SMTPConnection._processResponse (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:733:20)
at SMTPConnection._onData (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:529:14)
at Socket._socket.on.chunk (/var/www/node/node_modules/nodemailer/lib/smtp-connection/index.js:481:47)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
code: 'EAUTH',
response: '535 Authentication Failed for email#godaddydomain.com. User does not have any relays assigned.',
responseCode: 535,
command: 'AUTH PLAIN' }
Any thoughts or suggestions would be really appreciated.
It seems like this error is related to the hostname. Try this one:
var transporter2 = nodemailer.createTransport({
service: 'Godaddy',
host: "smtpout.secureserver.net",
secureConnection: true,
port: 465
})
I was facing the same error. I changed configuration to the following:
var transporter = nodemailer.createTransport({
host: "smtpout.secureserver.net",
secure: true,
secureConnection: false, // TLS requires secureConnection to be false
tls: {
ciphers: "SSLv3",
},
requireTLS: true,
port: 465,
debug: true,
auth: {
user: "xxxx#yyyy.com",
pass: "pass",
},
});
It worked like a charm for me.
In case anyone's still looking for this (as I have been the last few days!), I found all the server information GoDaddy was suggesting was completely wrong. Found the answer in cPanel - if you go to the main bit, and click on 'Email Accounts', on your account it should have an option on the right saying 'Connect Devices'. Click on that and it'll give you the correct server and port information for what you need, which for me ended up being this:
var transporter = nodemailer.createTransport({
host: "sxb1plzcpnl487525.prod.sxb1.secureserver.net",
secure: true,
secureConnection: true,
port: 465,
auth: {user: "yourcpanelusername#yourdomain.com", pass: "yourcpanelpassword"},
tls: {rejectUnauthorized: false}
});
Took me forever to find that specific weird server it needed. Hope this helps. Apologies for formatting, it's my first post here but I wanted to help as I've spent days on this now.

Connection timeout error when sending mail from Zohomail using Nodemailer

Below is my Node app.js code. With these settings, I am receiving a connection timeout error. Any idea what I am missing here?
var nodemailer = require("nodemailer");
var transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true, // use SSL
auth: {
user: '<myemail#example.com>',
pass: '<myemailpassword>'
}
});
var mailOptions = {
from: "<fromemail#example.com>",
to: "<toemail#example.com>",
subject: "Hello",
generateTextFromHTML: true,
html: { path: './tmpl.html' }
};
transporter.sendMail(mailOptions, function(error, response) {
if (error) {
console.log(error);
} else {
console.log(response);
}
transporter.close();
});
Error shown
{ Error: Connection timeout
at SMTPConnection._formatError (/home/ubuntu/workspace/mailapp/node_modules/nodemailer/lib/smtp-connection/index.js:557:19)
at SMTPConnection._onError (/home/ubuntu/workspace/mailapp/node_modules/nodemailer/lib/smtp-connection/index.js:530:20)
at Timeout._connectionTimeout.setTimeout (/home/ubuntu/workspace/mailapp/node_modules/nodemailer/lib/smtp-connection/index.js:248:18)
at ontimeout (timers.js:380:14)
at tryOnTimeout (timers.js:244:5)
at Timer.listOnTimeout (timers.js:214:5) code: 'ETIMEDOUT', command: 'CONN' }
Can anyone please help me?
Some cloud providers disable ports like 465 and 587, try using port 2525 instead of 465.
Update
Since you're using Cloud9 for this I found out they have blocked all outbound smtp calls from their servers. If you need to send anyways you need to choose another cloud provider or use one of their recommended services.
https://community.c9.io/t/how-can-i-send-email-from-my-app/1262

Resources