How to Send Emails with Firebase function and AWS SES service.

Lonare
3 min readJun 30, 2023

Step 1: Set up Firebase Project

  • Create a new Firebase project or use an existing one.
  • Install the Firebase CLI (Command Line Interface) if you haven’t already.

Step 2: Set up AWS SES

  • Create an AWS account if you don’t have one.
  • Go to the AWS Management Console and search for “SES” to open the Simple Email Service dashboard.
  • Follow AWS SES documentation to verify your domain and set up email sending permissions.

Step 3: Configure Firebase Cloud Functions

  • Open your Firebase project and navigate to the Functions section.
  • Install the necessary dependencies by running the following command in your project directory:
  • Install packages and build functions. I’m using Yarn, you can use npm if you want.
cd functions/
yarn OR npm install

The code will go to functions folder, then installs packages with yarn / npm.

npm install aws-sdk

Step 4: Create a Firebase Cloud Function

  • In your Firebase project, create a new JavaScript file for your Cloud Function, e.g., “sendEmail.js”.
  • Import the required modules at the top of your file:
const functions = require('firebase-functions');
const AWS = require('aws-sdk');

Step 5: Set up AWS SES Credentials

  • Add your AWS SES credentials to the Firebase Cloud Function. You can do this by creating environment variables or directly adding them to the function.
const ses = new AWS.SES({
region: 'your_aws_region',
accessKeyId: 'your_aws_access_key_id',
secretAccessKey: 'your_aws_secret_access_key',
});

Step 6: Define the Firebase Cloud Function

  • Write the code for sending an email using AWS SES within your Firebase Cloud Function.
exports.sendEmail = functions.https.onRequest(async (req, res) => {
const params = {
Destination: {
ToAddresses: ['recipient@example.com'], // Add recipient email addresses
},
Message: {
Body: {
Text: {
Data: 'Hello, this is the email content!', // Add your email content here
},
},
Subject: {
Data: 'Firebase Cloud Function Email', // Add your email subject here
},
},
Source: 'sender@example.com', // Add the sender email address
};

try {
await ses.sendEmail(params).promise();
res.status(200).send('Email sent successfully');
} catch (error) {
console.error('Error sending email:', error);
res.status(500).send('Error sending email');
}
});

Step 7: Deploy the Firebase Cloud Function

  • Deploy the Firebase Cloud Function by running the following command in your project directory:
firebase deploy --only functions

Step 8: Test the Firebase Cloud Function

  • After the deployment is successful, you can test the Firebase Cloud Function by accessing its provided URL endpoint.
  • Open a browser or use a tool like Postman to make an HTTP request to the Cloud Function’s URL.

Congratulations! You have successfully set up a Firebase Cloud Function to send emails using AWS SES.

You can now integrate this functionality into your application or trigger the Cloud Function from other parts of your Firebase project as needed.

The above code is in AWS sdk v2 if you want to convert it into v3 here is the documentation

Now when you run your function you will receive an email:

Any questions, comment below. Happy Coding :)

--

--

Lonare

Imagination is the key to unlock the world. I am trying to unlock mine.