数据命令失败:421 4.3.0临时系统问题。稍后再试(10)-GSMTP

发布于 2025-02-01 16:47:31 字数 2754 浏览 2 评论 0原文

我已经在Firebase上部署了HTTPS功能,当我尝试批量发送电子邮件时,我总是会遇到此错误。我正在使用NodeMailer池发送电子邮件。

错误:数据命令失败:421 4.3.0临时系统问题。稍后再试(10)-GSMTP

客户端代码:

export const sendEmails = async () => {
    // Fetch All Users
    let users = []
    let data = await db.collection('users')

    data.forEach(e => {
        let data = e.data()
        let email = data['email']
        users.push({ email })
    })

    // Divide users into multiple batches of 25
    let batches = [[]], num = 0, batchSize = 25

    Promise.all(users.map(batch => {
        if (batches[num].length < batchSize) {
            batches[num].push(batch)
        } else {
            batches.push([batch])
            num++
        }
    }))

    // Send Email Request for each batch with some cooldown time. 
    Promise.all(batches.map((batch, index) => {
        setTimeout(async () => {
            await sendBroadcast(batch)
        }, 2000 * index)
    }))
}

export const sendBroadcast = async (users) => {
    const url = base + "sendBroadcast?"
    const body = JSON.stringify({ users })
    return await fetch(url, { method: "POST", body })
}

服务器端代码:

let transporter = nodemailer.createTransport({
    service: 'gmail',
    pool: true,
    maxConnections: 20,
    maxMessages: 500,
    auth: {
        user: 'SOME EMAIL',
        pass: 'SOME PASSWORD',
    },
})

exports.sendBroadcast = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).https.onRequest((req, res) => {
    cors(req, res, () => {
        const body = JSON.parse(req.body)
        const users = body.users

        console.log('info', 'Send email to ' + users.length + ' users')

        users.forEach(function (to, index) {
            var msg = {
                from: 'SOME EMAIL',
                to: to.email,
                subject: "SUBJECT",
                html: "<h2> SOME HTML </h2>",
            }

            return transporter.sendMail(msg, (erro, info) => {
                if (erro) {
                    console.log('error', erro.message, 'failed to send to ' + email)
                    return res.status(500).send(erro.toString())
                }
                return res.send('sent')
            })

        })
    })
})

这是因为Gmail每天限制了电子邮件数吗?我在某个地方读到,如果我在每封电子邮件后添加冷却器,我也尝试了它,但是我也尝试了“客户网络插座在建立安全的TLS连接之前已断开连接”错误。这是该问题的堆栈溢出线程: firebase> firebase> firebase> firebase> firebase函数错误“通过NodeMailer发送批量电子邮件,在确定安全TLS连接之前已断开连接”

I have deployed an HTTPS Function on Firebase, and I always get this error when I'm trying to send emails in bulk. I am using nodemailer pool to send emails.

Error: Data command failed: 421 4.3.0 Temporary System Problem. Try again later (10) - gsmtp

Client-side code:

export const sendEmails = async () => {
    // Fetch All Users
    let users = []
    let data = await db.collection('users')

    data.forEach(e => {
        let data = e.data()
        let email = data['email']
        users.push({ email })
    })

    // Divide users into multiple batches of 25
    let batches = [[]], num = 0, batchSize = 25

    Promise.all(users.map(batch => {
        if (batches[num].length < batchSize) {
            batches[num].push(batch)
        } else {
            batches.push([batch])
            num++
        }
    }))

    // Send Email Request for each batch with some cooldown time. 
    Promise.all(batches.map((batch, index) => {
        setTimeout(async () => {
            await sendBroadcast(batch)
        }, 2000 * index)
    }))
}

export const sendBroadcast = async (users) => {
    const url = base + "sendBroadcast?"
    const body = JSON.stringify({ users })
    return await fetch(url, { method: "POST", body })
}

Server-side code:

let transporter = nodemailer.createTransport({
    service: 'gmail',
    pool: true,
    maxConnections: 20,
    maxMessages: 500,
    auth: {
        user: 'SOME EMAIL',
        pass: 'SOME PASSWORD',
    },
})

exports.sendBroadcast = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).https.onRequest((req, res) => {
    cors(req, res, () => {
        const body = JSON.parse(req.body)
        const users = body.users

        console.log('info', 'Send email to ' + users.length + ' users')

        users.forEach(function (to, index) {
            var msg = {
                from: 'SOME EMAIL',
                to: to.email,
                subject: "SUBJECT",
                html: "<h2> SOME HTML </h2>",
            }

            return transporter.sendMail(msg, (erro, info) => {
                if (erro) {
                    console.log('error', erro.message, 'failed to send to ' + email)
                    return res.status(500).send(erro.toString())
                }
                return res.send('sent')
            })

        })
    })
})

Is this because gmail limits number of emails per day? I read somewhere that it would work if I added cooldowns after each email, I tried that as well but I was getting "Client network socket disconnected before secure TLS connection was established" error. Here's the Stack Overflow thread for that issue: Firebase Function Error "Client network socket disconnected before secure TLS connection was established" when sending bulk emails via Nodemailer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文