Monday, 5 June 2017

nodemailer gmail closed Connection closed with high number of mails

I have exactly the same issue as this post : Send multipe emails using nodemailer and gmail

When sending too many emails with nodemailer and gmail, I get a 421 error, referring to too many concurrent sessions.

What can i do to avoid opening too many sessions ?

I already contact google who confirm me I wasn't blocked by any limitation ( I didn't reach the number of daily mail and there is no limit on mail/minute). I tried to wait for each mail to be sent before sending a new one; to create and close new transport at each mail, but I keep getting this error after approximately the 100th email.

Here the complete error:

{ [Error: Mail command failed: 421 4.7.0 Try again later, closing 
connection. (MAIL) e17sm2124566ede.14 - gsmtp]
code: 'EENVELOPE',
response: '421 4.7.0 Try again later, closing connection. (MAIL) 
e17sm2124566ede.14 - gsmtp',
responseCode: 421,
command: 'MAIL FROM' }

And my code :

Nodemailer settings :

function setMailTransport () {
  return nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    ignoreTLS: true,
    auth: {
      xoauth2: xoauth2.createXOAuth2Generator({
        user: 'xxxxxx',
        clientId: 'xxxxxx',
        clientSecret: 'xxxxxx',
        refreshToken: 'xxxxxx'
      })
    }
  }))
}

Sending unique mail :

async function sendEmail (mail) { 
  // mail is an object {from, to, subject, text, html}
  const transport = setMailTransport()
  try {
    await transport.sendMail(mail)
    await transport.close()
    return 1
  } catch (err) {
    console.log(err)
    await transport.close()
    return 0
  }
}

Recursive async/await function to wait mail be sent before sending a new one :

async function sendAlerts (mails, index, numberOfMailSent) {
  // mails is an array of mail object, index start at 0  
  // numberOfMailSent is just a counter to know how many mails have been sent
  if (index >= mails.length) return numberOfMailSent
  const mail = mails[position]
  const newMailSent = await sendEmail(mail)
  return sendAlerts(mails, index + 1, numberOfMailSent + newMailSent)
}

Any idea of where I could have been wrong or on any other way to send more than 100 mails ?



via zevanb