1

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

Inaara Kalani
  • 265
  • 7
  • 24
  • Have you referred to these link? https://stackoverflow.com/questions/61633032/421-4-3-0-temporary-system-problem-try-again-later-10, https://stackoverflow.com/questions/64060781/nodemailer-custom-smtp-not-working-on-firebase-functions/65034701#65034701 – Sandeep Vokkareni May 27 '22 at 12:29
  • Yes I have @SandeepVokkareni. As you can see in my code, I have used pooling. As I've also mentioned, I added a cooldown and encountered a different error. I have added the link of Stack Overflow for this error as well in my question. – Inaara Kalani May 30 '22 at 12:26
  • Have you checked with this article for [email sending limits](https://support.google.com/a/answer/166852) and the [Gmail SMTP errors and codes](https://support.google.com/a/answer/3726730) – Sandeep Vokkareni May 31 '22 at 06:24
  • Yes I have. The article does not provide the solution to my problem @SandeepVokkareni. Please provide an answer if you can, because simply sharing links is not helping. – Inaara Kalani May 31 '22 at 09:00
  • Does this answer your question? [Gmail SMTP error - Temporary block?](https://stackoverflow.com/questions/39097834/gmail-smtp-error-temporary-block) – paulsm4 Oct 24 '22 at 01:02

0 Answers0