How to Send Emails with Serverless AWS Lambda: Complete Guide
By Braincuber Team
Published on April 13, 2026
Serverless computing lets you build and run applications without managing servers. This complete tutorial shows you how to use the Serverless Framework with AWS Lambda to create a function that fetches data from an API and sends it via email. You will learn to deploy serverless functions, manage environment variables, and automate email sending - all without provisioning or managing servers.
What You'll Learn:
- Understanding serverless and AWS Lambda concepts
- Installing and configuring Serverless Framework
- Step by step guide to creating Lambda functions
- Complete tutorial on nodemailer for email sending
- Beginner guide to managing environment variables
- How to deploy serverless functions to AWS
Understanding Serverless Computing
Serverless is a term that has been increasing in popularity in recent years. Despite the name, serverless does not mean there are no servers - there are servers, you just do not have to think about it. Serverless computing allows you to build and run applications and services without thinking about servers.
The Serverless Framework is the easiest method to get started with serverless computing. It provides a unified experience for deploying functions across different cloud providers like AWS, Google Cloud Platform, and Azure.
Note: Serverless Framework vs AWS Lambda
The Serverless Framework (sls) is a tool that helps you deploy and manage AWS Lambda functions. AWS Lambda is the actual serverless compute service where your functions run.
Serverless Vocabulary
There are several key terms you need to understand when working with the Serverless Framework:
Service
A collection of code that is all served from a single place. Can contain one or more functions.
Stage
The type of environment you are running, usually divided into dev and prod.
Function
A piece of code that executes when called. Can be part of a Service.
Provider
The place where your service is deployed, e.g., AWS, GCP, Azure.
Setting Up the Serverless Framework
Before you can deploy serverless functions, you need to set up your development environment. This step by step guide walks you through the complete setup process.
Installing the Serverless Framework
The Serverless Framework is installed via npm. If you do not have npm installed, you will need to install Node.js first. Then run the following command:
npm i -g serverless
Creating a New Service
Create a new service using the aws-nodejs template. This provides a basic starting point for your serverless application:
serverless create --template aws-nodejs --path my-service
This creates three files in your directory:
- handler.js - Your function code
- serverless.yml - Configuration file
- .gitignore - Git ignore rules
Deploying to AWS
One of the best things about the Serverless Framework is how early you can deploy. Unlike traditional applications where deployment comes at the end, with sls you can deploy early and often.
Prerequisite
You need to configure AWS credentials before deploying. Follow the Serverless AWS Quickstart Guide to set up your credentials.
Deploy Command
sls deploy
When you deploy, the Serverless Framework creates a CloudFormation stack that manages your service. You can view your deployed functions in the AWS Lambda Console.
Creating the Email Sending Function
Now let us create a function that sends emails. We will use nodemailer - a popular Node.js library for sending emails.
Installing nodemailer
npm init -y
npm i nodemailer
Creating the Email Handler
Create a file called emailHandler.js and add the following code. This example uses the Chuck Norris API to fetch a random joke and email it:
const nodemailer = require('nodemailer');
const { MAIL_HOST, MAIL_PORT, MAIL_USER, MAIL_PASS } = process.env;
const transport = nodemailer.createTransport({
host: MAIL_HOST,
port: MAIL_PORT,
auth: { user: MAIL_USER, pass: MAIL_PASS },
});
module.exports.sendEmail = async event => {
const response = await fetch('https://api.chucknorris.io/jokes/random');
const joke = await response.json();
const html = 'Joke of the Day
' + joke.value + '
';
await transport.sendMail({
from: '"Chuck Norris" ',
to: 'your-email@example.com',
subject: 'Daily Joke',
html,
});
return { statusCode: 200, body: JSON.stringify({ message: joke.value }) };
};
Updating serverless.yml
Update your serverless.yml to include the new function and environment variables. Note that you will need to create an env.yml file with your email credentials.
functions:
sendEmail:
handler: emailHandler.sendEmail
hello:
handler: handler.hello
provider:
name: aws
runtime: nodejs18.x
stage: dev
environment:
MAIL_HOST: \${file(env.yml):dev.MAIL_HOST}
MAIL_PORT: \${file(env.yml):dev.MAIL_PORT}
MAIL_USER: \${file(env.yml):dev.MAIL_USER}
MAIL_PASS: \${file(env.yml):dev.MAIL_PASS}
Install Serverless Framework
Install the Serverless Framework globally using npm: npm i -g serverless
Create Service
Create a new service using the aws-nodejs template.
Configure Credentials
Set up AWS credentials and create env.yml for environment variables.
Deploy and Test
Deploy with sls deploy and test locally with sls invoke local -f sendEmail
Frequently Asked Questions
What is the Serverless Framework?
The Serverless Framework is a tool that helps you build and deploy serverless applications. It works with AWS Lambda, Google Cloud Functions, and Azure Functions.
How much does AWS Lambda cost?
AWS Lambda includes a free tier with 400,000 GB-seconds of compute time per month. Beyond that, you pay based on invocation count and execution time.
What is nodemailer?
Nodemailer is a Node.js library for sending emails. It supports simple SMTP transport and various authentication methods.
Can I test serverless functions locally?
Yes, use sls invoke local -f functionName to test your functions locally without deploying to AWS.
What is Mailtrap used for?
Mailtrap is a tool for testing emails during development. It catches emails sent by your application so you can preview them without setting up a real mail server.
Need Help with Serverless Development?
Our experts can help you build and deploy serverless applications on AWS. Get free consultation for your serverless projects today.
