Email form sender with AWS Lambda, Brevo & reCAPTCHA

Background

In my previous article Email form sender with Nuxt3, Cloudflare, Brevo & reCAPTCHA I showed how to use Nuxt3, Brevo & reCAPTCHA with Cloudflare.

In this article I will show how to use AWS Lambda instead of Cloudflare to send the email.

AWS Lambda configuration

You're going to need an AWS Lambda function so go ahead and create one. I was able to get away with 128MB of memory and a 20 second timeout. You'll also need to configure it as a function URL with the "NONE" auth type to allow anonymous posting to it. Also remember to enable cross-origin resource sharing and set the allowed origin to your website's domain(s) with POST method and you probably want to allow all headers with "*".

This step will also give you the function URL you'll need to copy into your website's form action attribute.

Code

Here's the code to paste into your AWS Lambda function.

/* global fetch */

export const handler = async (event, context) => {
  console.log("Starting contact form function");

  // Validate parameters
  const { firstName, lastName, email, message, token } = JSON.parse(event.body)
    
  if (!firstName || !lastName || !email || !message || !token) {
    return { statusCode: 400, body: JSON.stringify({ statusMessage: "Missing required fields" })}
  }
  
  // Validate captcha
  const verifyResponse = await fetch("https://www.google.com/recaptcha/api/siteverify", {
    method: "POST",
    headers: {
      "content-type": "application/x-www-form-urlencoded",
    },
    body: `secret=${process.env.RECAPTCHA_SECRET}&response=${token}`,
  })
  const verifyResponseBody = await verifyResponse.json()
  if (!verifyResponse.ok) {
    return "Unable to validate captcha at this time."
  }

  if (verifyResponseBody.success !== true) {
    return "Invalid captcha response."
  }
  
  // Send email
  const emailSendResponse = await fetch("https://api.brevo.com/v3/smtp/email", {
    method: "POST",
    headers: {
      accept: "application/json",
      "api-key": process.env.BREVO_KEY,
      "content-type": "application/json",
    },
    body: JSON.stringify({
      to: [{ email: process.env.EMAIL_TO_ADDRESS, name: process.env.EMAIL_TO_NAME }],
      sender: {
        email: email,
        name: `${firstName} ${lastName}`,
      },
      subject: process.env.EMAIL_SUBJECT,
      textContent: message,
    }),
  })

  if (!emailSendResponse.ok) {
    return "Message could not be sent at this time."
  }

  return "Message sent!"
}

Environment Variables

Finally you'll need to configure a few environment variables. These are:

  • RECAPTCHA_SECRET - Your reCAPTCHA secret key
  • BREVO_KEY - Your Brevo API key
  • EMAIL_TO_ADDRESS - The email address you want to send the email to
  • EMAIL_TO_NAME - The name of the person you want to send the email to
  • EMAIL_SUBJECT - The subject of the email

Conclusion

I hope you find this useful. Of course you could always use AWS's own SES service to send the email but there are plenty of examples of how to do that already.

Disclaimer

I am a Brevo partner eligible for commission on sales I make directly with them however I do not receive any compensation for this article or have link-based referrals for commission.

0 responses