繁体   English   中英

加快 Nodemailer 发送电子邮件的速度

[英]Speed up Nodemailer send Emails

我有一个类似这样的 NodeJS 代码:(Netlify)

const sendEmail = require('./sendmail')
async function checkout() {
    try {
        await someFunction()
    } catch(e) {}
    try {
        await someOtherFunction()
    } catch(e) {}
    try {
        await sendEmail.sendCustomer(subject, body, to)  // await or not?
        await sendEmail.sendOurself(subject, body, to)   // await or not?
    }
    finally {
        return {
            statusCode: 200
        }
    }
}

和 function 发送电子邮件:

const nodemailer = require('nodemailer')
async function sendEmail(subject, body, to) {

    const transporter = nodemailer.createTransport({
        host: HOST,
        port: PORT,
        secure: SECURE,
        auth: { user: AUTH.user, pass: AUTH.pass,}
    });

    const mailOptions = {
        from: AUTH.user,
        to: to,
        subject: subject,
        html: body,
    };

    return await transporter.sendMail(mailOptions)
};

exports.sendEmail = sendEmail

问题是,function 发送电子邮件需要很长时间。 有时 5 秒,有时两封电子邮件长达 12 秒。

我怎样才能加快速度? 如您所见,我并不真正关心来自 nodemailer 的响应。 所以我尝试在最后一个 try 块中删除 await 并使 function sendEmail 同步。 但是,电子邮件没有被发送。 我认为这是因为当我返回 200 时 function 停止运行。我怎样才能加快这个过程?

我最终得到了 Promise.all 而不是等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM