Send SMTP Email using AWS Lambda

Send SMTP Email Using AWS Lambda

Yeah, I made my static website with a contact form submission (with a mailer api). I build beautiful websites and if static, I’ll host in AWS S3. But the hectic is the contact form where the contact message needs to be send as email requires a server. And Yeah, I made this working creating an mailer api to Send SMTP Email using AWS Lambda Function.

This blog helps you to create a simple mailer api to send SMTP email using AWS Lambda and API Gateway & integrate with your static website hosted at Amazon S3 or Firebase Hosting. AWS Lambda is a service that runs your code without requiring you to manage server and lets create it.

Create My First AWS Lambda Function

You need an AWS account and if you don’t have, please create it and enjoy one year free trial.

AWS Lambda Getting Started

AWS Lambda Getting Started


Step 1.0: If this is your first time creating lambda function, you’ll see the above screen and proceed with a click on “Get Started Now

Step 1.1: It’s time to create a new function. Under “Select Blueprint”, select “Blank Function“.

Step 1.2: We can skip the triggers for now and lets do that later. For “Configure triggers”, simply press “Next” button.

Step 1.3: Now the section “Configure functions” is the place you actually create and code.

  • Fill the details like Name as ‘mailer‘, Description as ‘Send SMTP Email‘ and select the Runtime as ‘Python 3.6‘.
  • Below the code workspace, under “Lambda function handler and role”, select ‘Create new roles from template(s)‘ as Role and provide a Rolename as “LambdaRunner” if this is your first time. You are creating a new role and this name can be anything.
    If you already have role created, select “Choose an existing role”. Choose “Next”.

Step 4: You can review the details in “Review” section, and click “Create Function” button to complete the setup.

Step 5: Click on the blue colored “Test” button to test your function. You’ll see a similar screen as below (Output Highlighted)

My First AWS Lambda Function

My First AWS Lambda Function

Code to Send SMTP Email using AWS Lambda

Step 2.1: Just copy the below code and paste it in the code workspace.

If the above code is not visible, please click here

Step 2.2: We’ll be getting the inputs like email ‘subject’ and ‘body’ via api query string and request body respectively.

  • For testing, choose the “Configure test event” from “Actions” dropdown next to blue colored “Test” button.
  • Choose “API Gateway AWS Proxy” from ‘Sample event template’ dropdown.
  • Change the “body” param as required and add a “subject” in ‘queryStringParameters’ as highlighted in below image and press “Save” (not “Save and Test”).

Input Test Event for API Gateway

Input Test Event for API Gateway


Step 2.3: I’ve made the details like smtp details and credentials as Environment variables so that you don’t need to change the code. Set the required Environment variables as below and press blue colored “Save and test” button.

You can use any email provider of your choice like gmail or yahoo or outlook or zoho mail.

SMTP Email Environment Variables

SMTP Email Environment Variables

Creating App Specific Password in Google

You may entered correct password, but you might have getting the below error if provider is Gmail or similar error if others.

(535, b'5.7.8 Username and Password not accepted. Learn more at
5.7.8  https://support.google.com/mail/?p=BadCredentials 24sm16308131qtt.53 - gsmtp')

This is because Google never allows to use your password for any clients. You need to create “App Password” for every application that wants to access yout gmail account.

Step 3.1: Go to the App Password dashboard and choose “Other (Custom name)” under ‘Select Device’ dropdown.

Google App Password Dashboard

Google App Password Dashboard

Step 3.2: Enter the name like “AWS Lambda” and click “Generate” button.

Google App Password Creation

Google App Password Creation

Step 3.3: Copy the 16 digit code and use it value for ‘PASSWORD’ environment variable seen in step 2.3.

Now press the blue colored “Save and test” button. This time the email should hit your inbox.

AWS Lambda Test Mail

AWS Lambda Test Mail

Configure Triggers as API Gateway

Step 4.1: Go to the “Triggers” tab located below the blue colored “Test” button and click on “Add trigger”
Step 4.2: Click on the grey bordered empty box and select “API Gateway”
Step 4.3: Choose the API name as default “LambdaMicroService“, Deployment stage as it’s default “prod” and Security as “Open” and Submit

AWS Lamda Trigger API Gateway

AWS Lamda Trigger API Gateway

Step 4.4: Copy the http url endpoint and hit it via Insomnia or Postman with subject as query param and email body as request body

API Gateway Mailer Endpoint

API Gateway Mailer Endpoint

That’s all You did it. An email should hit your inbox now.

API Gateway Test Mail

API Gateway Test Mail

Hope you have created an api to send SMTP email using AWS Lambda and API Gateway & integrate with your static website hosted at Amazon S3 or Firebase Hosting in contact forms.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *