skip to Main Content

Good day,
I have simple Nodejs program that is using Nodemailer:

var syncSql = require('sync-mysql');             
var nodemailer = require('nodemailer');   

var syncCon = new syncSql(
{ 
    host:'localhost', 
    user:'_nodeuser', 
    password:'_password', 
    database:'mydatabase'
});

var transporter = nodemailer.createTransport(
{
    service: 'gmail',
    auth: 
    {
        user: '[email protected]',
        pass: '_gmailAppPass'
    },
    logger: true
});

...
resp2 = syncCon.query(sql);

var mailOptions =
            {
                from:   '[email protected]',
                to:      resp2.email,                       
                subject:'_email subject',     
                html:   '_email body'
            };

            transporter.sendMail(mailOptions, function(error, info) {
                if(error) {
                    console.log('remind_old_action(): '+ error);
                } else {
                    console.log('Email sent: ' + info.response);   
                }
            });

on my local Win 11 machine it works:

[2023-03-20 11:33:08] DEBUG Creating transport: nodemailer (6.9.0; +https://nodemailer.com/; SMTP/6.9.0[client:6.9.0])
[2023-03-20 11:33:09] DEBUG Sending mail using SMTP/6.9.0[client:6.9.0]
[2023-03-20 11:33:10] DEBUG [0FD44uxJ0] Resolved smtp.gmail.com as 173.194.76.108 [cache miss]
[2023-03-20 11:33:10] INFO  [0FD44uxJ0] Secure connection established to 173.194.76.108:465
[2023-03-20 11:33:11] DEBUG [0FD44uxJ0] SMTP handshake finished
[2023-03-20 11:33:11] INFO  [0FD44uxJ0] User "[email protected]" authenticated
[2023-03-20 11:33:11] INFO  Sending message <[email protected]> to <_target_email>
[2023-03-20 11:33:12] INFO  [0FD44uxJ0] <501 bytes encoded mime message (source size 498 bytes)>
[2023-03-20 11:33:13] DEBUG [0FD44uxJ0] Closing connection to the server using "end"
Email sent: 250 2.0.0 OK  1679311993 v15-20020a05600c444f00b003edddce5b00sm4292772wmn.12 - gsmtp
[2023-03-20 11:33:13] INFO  [0FD44uxJ0] Connection closed

but when I run this program on Ubuntu 22.04 server, it does not work from there:

[2023-03-20 11:40:27] DEBUG Creating transport: nodemailer (6.9.1; +https://nodemailer.com/; SMTP/6.9.1[client:6.9.1])
[2023-03-20 11:40:27] DEBUG Sending mail using SMTP/6.9.1[client:6.9.1]
[2023-03-20 11:40:27] DEBUG [jZWnAXUE78U] Resolved smtp.gmail.com as 172.253.63.108 [cache miss]
[2023-03-20 11:42:27] ERROR [jZWnAXUE78U] Connection timeout
[2023-03-20 11:42:27] DEBUG [jZWnAXUE78U] Closing connection to the server using "destroy"
[2023-03-20 11:42:27] ERROR Send Error: Connection timeout
remind_old_action(): Error: Connection timeout

It seems the problem is not with Nodemailer itself.
I have also tried from express server (app) – same situation. The express app and sending mails both work from local machine. Express app itself works from Ubuntu server, but sending mails is not.
I’m also using Nginx as a reverse proxy for express app, played with config, switched it on/off – sending emails does not work in either case. Nodemailer can’t connect to smtp.gmail.com, gives an error after 120 sec of waiting.
What else could I check?

UPD: I have enabled 2FA on google account and created a "less secure" 16-char apps password. It works correctly from local win machine, no problem with authentication, but time outs on ubuntu server.

SOLVED: "It’s a trap!" (c)

The issue was that the ports 25/465 were blocked by cloud provider (Hetzner) and I unfortunately couldn’t imagine this and thought the problem was on my side.

2

Answers


  1. You are use gmail service for send mail, so you can create 16 digit password of your Gmail and this password is use

    var transporter = nodemailer.createTransport(
    {
        service: 'gmail',
        auth:
        {
            user: '[email protected]',
            pass: '16 digit _gmailAppPass'
        },
        logger: true
    });
    
    Login or Signup to reply.
  2. Check your Ubuntu 22.04 server’s firewall settings, and allow SMTP and DNS traffic.

    sudo ufw allow 53/tcp
    sudo ufw allow 53/udp
    sudo ufw allow out 25/tcp
    sudo ufw allow out 25/udp
    sudo ufw reload
    

    After allowing those ports nodemailer started working for me. Also try disabling your firewall completely to see if it will send any emails.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search