Nodemailer 不从 Ubuntu 服务器发送邮件

问题描述 投票:0回答:2

美好的一天, 我有一个使用 Nodemailer 的简单 Nodejs 程序:

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);   
                }
            });

在我本地的 Win 11 机器上它可以工作:

[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

但是当我在 Ubuntu 22.04 服务器上运行这个程序时,它在那里不起作用:

[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

看来问题不在于 Nodemailer 本身。 我也试过从快递服务器(应用程序) - 同样的情况。快递应用程序和发送邮件都在本地机器上工作。 Express 应用程序本身可以在 Ubuntu 服务器上运行,但不能发送邮件。 我还使用 Nginx 作为 express 应用程序的反向代理,使用配置,打开/关闭它——发送电子邮件在任何一种情况下都不起作用。 Nodemailer 无法连接到 smtp.gmail.com,等待 120 秒后出现错误。 我还能检查什么?

UPD:我在谷歌账户上启用了 2FA,并创建了一个“不太安全”的 16 字符应用程序密码。它在本地 win 机器上正常工作,身份验证没有问题,但在 ubuntu 服务器上超时。

node.js ubuntu gmail timeout nodemailer
2个回答
0
投票

您正在使用gmail服务发送邮件,所以您可以创建您的Gmail的16位密码,这个密码是使用

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

0
投票

检查您的 Ubuntu 22.04 服务器的防火墙设置,并允许 SMTP 和 DNS 流量。

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

允许这些端口后,nodemailer 开始为我工作。也可以尝试完全禁用防火墙,看看它是否会发送任何电子邮件。

© www.soinside.com 2019 - 2024. All rights reserved.