Function only returns 502

I previously had one function, a simple contact form that sends an email. It worked perfectly fine since initial deployment several months ago.

Today I’ve added a new function, which is exactly the same – just sends an email. I also took the time to convert to using environment variables. I have it working with netlify dev and it pulls the env correctly from my linked account. Both functions work locally.

When I deploy, sometimes the original function will work the first time but subsequent calls are always 502 “Task timed out after 10.01 seconds”. The new function always gives that response.

I’ve done a test to return the process.env and it does contain my variables.

The two function are nearly identical:

const nodemailer = require('nodemailer')

const autoReply = `<p>Dear LDi-iSC Team Member,</p>
<p>Thank you for your interest in the referral program! We will let you know when the candidate has been hired and then we will contact you about receiving the referral award. Word-of-mouth is the most successful way to hire called and qualified candidates, so we appreciation your recommendations. We could not do this without you!</p>
<p>Thank you,</p>
<p>Mobilization and Recruitment Team</p>`

exports.handler = (event, context, callback) => {
  let transporter
  let body

  try {
    body = JSON.parse(event.body)

    // Create reusable transporter object using the default SMTP transport
    transporter = nodemailer.createTransport({
      host: process.env.EMAIL_HOST,
      port: process.env.EMAIL_PORT,
      secure: false,
      auth: {
        user: process.env.EMAIL_USER,
        pass: process.env.EMAIL_PASS
  } catch (err) {
    return callback(null, {
      statusCode: 500,
      body: err.message

  try {
    // send mail with defined transport object
        from: process.env.EMAIL_ADDR,
        to: body.emp_email,
        subject: `Thanks for your referral`,
        html: autoReply
        from: process.env.EMAIL_ADDR,
        to: process.env.REFERRAL_TO_ADDR,
        subject: `Employee Referral Form Submission`,
        html: `<p>An employee has submitted the referral form:</p>
          <p><strong>Employee Name:</strong><br>${body.emp_name}</p>
          <p><strong>Employee Email:</strong><br>${body.emp_email}</p>
          <p><strong>Referral Name:</strong><br>${body.ref_name}</p>
          <p><strong>Referral Email:</strong><br>${body.ref_email}</p>`
    ]).then(() => {
      callback(null, {
        statusCode: 200,
        body: ''
  } catch (e) {
    return callback(null, {
      statusCode: 500,
      body: e.message

I’m totally clueless what’s going on. Thanks!

Here is the function log. As you can see the first call in the group works, afterwards all timeouts:

2:16:17 PM: Duration: 759.52 ms	Memory Usage: 69 MB	Init Duration: 68.20 ms	
2:23:14 PM: Duration: 10010.21 ms	Memory Usage: 69 MB	Init Duration: 70.16 ms	
2:23:14 PM: 2019-11-22T06:23:14.961Z 107942ed-76c7-47eb-9dfe-1bcbd9bd98d9 Task timed out after 10.01 seconds

2:26:28 PM: Duration: 10010.16 ms	Memory Usage: 27 MB	
2:26:28 PM: 2019-11-22T06:26:28.498Z 36af9e25-0cfe-41db-9f98-b75fdb3b54a7 Task timed out after 10.01 seconds

2:35:07 PM: Duration: 10010.23 ms	Memory Usage: 27 MB	
2:35:07 PM: 2019-11-22T06:35:07.979Z 4b7dce88-1df5-4c3a-bf35-2efdf7ac9274 Task timed out after 10.01 seconds

2:48:48 PM: Duration: 723.24 ms	Memory Usage: 69 MB	Init Duration: 70.07 ms	
2:49:22 PM: Duration: 10010.16 ms	Memory Usage: 69 MB	
2:49:22 PM: 2019-11-22T06:49:22.542Z 2488083e-2228-4d8c-a1af-fd567c1b4696 Task timed out after 10.01 seconds

2:51:41 PM: Duration: 10010.16 ms	Memory Usage: 27 MB	
2:51:41 PM: 2019-11-22T06:51:41.816Z 8892c4c2-b487-47ad-a175-81f442d19b34 Task timed out after 10.01 seconds

I think it might be limitations with the email… Investigating more

Hi Grant,

I doubt it has anything to do with email specifically. This is your problem:


502’s happen with functions that try to run for more than 10 seconds, since that’s where we cut you off.

What I mean is that sending via SMTP is taking too long, causing the timeout. I changed it to send into a request to a webhook and it works every time within 10 seconds. Something with my SMTP provider is causing the problems.

@grantholle, that makes sense. Thank you for clarifying why the timeout is occurring.

If there are other questions about this issue, please let us know.