0

I am sending bulk emails from Firebase HTTP Request using Nodemailer. Some of the emails are always missed out and it returns with this error:

Client network socket disconnected before secure TLS connection was established

Here is my code for Client-Side:

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:

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>",
            }

            setTimeout(() => {
                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')
                })
            }, 10000 * index)

        })
    })
})

I was getting the Error "Temporary System Problem. Try again later (10)" before, so I added the cooldown time and sent multiple requests in batches. But now I'm getting the TLS error. I have tried a lot of solutions online, but there is always some error due to which all emails don't get sent.

Inaara Kalani
  • 51
  • 1
  • 5

0 Answers0