0

I try to create a lambda function for sending the email. The problem is I cannot get the email template path on Lambda. The error I received is:

"errorMessage": "ENOENT: no such file or directory, open '/var/task/email_template.ejs'"

Here are my project structure and my code: Project structure

service.js:

export const generateEmail = async (subject, text, email) => {
  try {
    const pathName = process.env.LAMBDA_TASK_ROOT
      ? path.resolve(process.env.LAMBDA_TASK_ROOT, 'email_template.ejs')
      : path.resolve(__dirname, 'email_template.ejs')

    const data = await ejs.renderFile(pathName, {
      name: 'John Doe'
    })

    return {
      from: '"Service" <[email protected]>',
      to: email,
      subject: subject,
      text: text,
      html: data
    }
  } catch (err) {
    throw err
  }
}

sendMail.js

exports.handler = async (event, context, callback) => {
  if (event.httpMethod === 'POST' && event.body) {
    const req = JSON.parse(event.body)
    const { name, email } = req

    if (!email.match(/^[^@]+@[^@]+$/)) {
      console.log('Not sending: invalid email address', body.email)
      return callback(null, {
        statusCode: 400,
        body: JSON.stringify({
          error_code: 'invalid_email',
          message: 'Cannot send. Your email is invalid'
        })
      })
    }

    const config = {
      accessKeyId: process.env.ACCESS_KEY,
      secretAccessKey: process.env.SECRET_KEY
    }

    const transporter = nodemailer.createTransport(ses(config))
    const mailOptions = await generateEmail('Hello ✔', 'Hello world', email)

    try {
      await transporter.sendMail(mailOptions)
      return callback(null, {
        statusCode: 204
      })
    } catch (err) {
      return callback(null, {
        statusCode: 400,
        body: JSON.stringify({
          error_code: 'general_error',
          message: 'Please try again later',
          full_error: err
        })
      })
    }
  }

  return callback(null, {
    statusCode: 400,
    body: JSON.stringify({
      error_code: 'missing_parameters',
      message: 'Please provide your name and email'
    })
  })
}

The generateEmail function works perfectly on locally but it did not work on lambda function.

Any help would be appreciated, thanks!

3
  • Your template is probably in path.resolve(process.env.LAMBDA_TASK_ROOT, 'src/email_template.ejs') Commented Jul 2, 2019 at 14:33
  • Thanks Paulpro, I also tried this, but it did not work either Commented Jul 2, 2019 at 14:44
  • I think it could be the case where my email_template.ejs file was not built by webpack and deployed. Commented Jul 2, 2019 at 15:03

1 Answer 1

2

I finally found the answer by myself. The reason because when webpack build, it ignores my email_template.ejs file.

So I add this line to my webpack.config.js to make it exist on my zip output and It works perfectly.

  (...),
  plugins: [
    new CopyWebpackPlugin([{ from: './src/email_template.ejs', to: './src' }])
  ]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.