电子邮件未使用nodemailer发送

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

我使用 smtp.emailcloud.smartping.io 作为我的 SMTP 服务提供商。 电子邮件已在我的本地主机上成功发送。 但未在我的服务器上发送时出现以下错误:

SendMail error: ==>> Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
   errno: -104,
   code: 'ESOCKET',
   syscall: 'read',
   command: 'CONN'
}

我的代码如下:

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

const sendEmail = async (emailData) => {
    try {
        const { senderEmail, sendTo, subject, emailBody, attachmentName } = emailData;

        const attachmentPath = path.join(process.cwd(), 'uploads/attachments', attachmentName);

        const mailOptions = {
            from: senderEmail,
            to: sendTo,
            subject: subject,
            html: emailBody,
            attachments: attachmentName !== 'null'
                ? [
                    {
                        filename: attachmentName,
                        path: attachmentPath
                    }
                ]
                : []
        }
        if (emailData.cc) {
            mailOptions['cc'] = emailData.cc;
        }
        console.log('mailOptions', mailOptions);
        // transporter.sendMail(mailOptions)
        transporter.sendMail(mailOptions)
            .then(info => console.log('Email sent: ==>>', info))
            .catch(error => console.error('SendMail error: ==>>', error));
    } catch (error) {
        console.log('error -->>', error);
        Sentry.captureMessage("Something went wrong in sendEmail util function");
        Sentry.captureException(error);
    }
};

我尝试了以下配置更改:

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: 465,
    secure: true,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

我尝试了谷歌的几种解决方案,但没有成功。

如果你能帮我解决这个问题那就太好了。

谢谢你

node.js smtp nodemailer
1个回答
0
投票

您可以检查一下服务器网络配置和防火墙配置吗? 之前我在 NodeJS 服务器上遇到过类似的问题,但在使用

ufw
firewalld
修复防火墙配置后,一切都很好。

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