Serverless email notifications with Sendgrid
Tags: serverless, sendgrid
Published: 2020-12-31 11:34:22

Email notifications have become an integral part of our systems. Common use-cases include sending reports, notifying build success, system failure events, etc. This tutorial demonstrates a solution using Google Cloud Functions to send emails using Sendgrid.

## Goals
- Scalable
- Ease of content modification
- Reliable delivery

## Setup
A sender will provide the trigger a cloud function with params. The cloud function will make a call to Sendgrid which in turn will send emails to the receiver.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yg2lj0caksaqvau06eew.png "System")
*System*

## Setting up a Sendgrid account
You can [Sign up](https://signup.sendgrid.com/) for a Sendgrid Account to manage our details. It offers a generous [free plan](https://sendgrid.com/pricing/) which is enough for our demo. Choose the create single sender option, in case you want to use your own domain the instructions can be found [here](https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/).

![Sender Identity Type](https://dev-to-uploads.s3.amazonaws.com/i/hnt42aip4lufz0b9s9az.png "Sender Identity Type")
*Sender Identity Type*


![Creating a sender](https://dev-to-uploads.s3.amazonaws.com/i/dj0j5l1ivpjfpqjyttir.png "Creating a sender")
*Creating a sender*

You’ll receive an email for sender confirmation as configured, click on Verify Single Sender.

![Sender Verification](https://dev-to-uploads.s3.amazonaws.com/i/kd6srn9cg9rd87p8a5au.png "Sender Verification")
*Sender Verification*

## Obtaining an API key
Sending emails via APIs is the recommended way for apps rather than SMTP. So, we’ll use the Web APIs which will be called by the SDKs. Let’s get the API Key for our account.

![Choosing the Integration type](https://dev-to-uploads.s3.amazonaws.com/i/4kfal6zd2dw0o23rhgrf.png "Choosing the Integration type")
*Choosing the Integration type*


![Choosing the language](https://dev-to-uploads.s3.amazonaws.com/i/cz90t0t4v0fjz8e9bgij.png "Choosing the language")
*Choosing the language*


![Obtain the API Key](https://dev-to-uploads.s3.amazonaws.com/i/5rriovi2oud8xfoq700f.jpg "Obtain the API Key")
*Obtain the API Key*

Great, now let’s start coding!

## Library function for Sendgrid
Let’s create a library that will enable us to send emails via Sendgrid. This way our code is decoupled and can be used by a simple library import.

Install the Sendgrid SDK via npm.

```
# install Sendgrid dependency
npm install --save @sendgrid/mail
```

Now let’s write a library that abstracts the SDK calls of Sendgrid.

{% gist https://gist.github.com/thefellowcoder/e924f87f1f255ff8f1879658041cacd1 file=sendgrid.js %}

Let’s test the lib code with our API Key.

{% gist https://gist.github.com/thefellowcoder/e924f87f1f255ff8f1879658041cacd1 file=sendgrid-test.js %}

Great! now we have a working library code, let’s use it in our cloud function.

## Cloud function
Let’s create a simple cloud function that uses this library and sends an email.

{% gist https://gist.github.com/thefellowcoder/e924f87f1f255ff8f1879658041cacd1 file=sendgrid-demo.js %}

Here, we have configured the API Key as an env variable, thus we need to make sure that it is available at runtime. Make sure that the lib code is also part of the deployment, else it will throw an error.

Sample command to deploy the cloud function

```
gcloud functions deploy sendgridDemo --trigger-http --entry-point=sendgridCF \
--region=us-central1 --runtime=nodejs10 --set-env-vars API_KEY='123'

```
Once deployed make sure to change the env variable value with the actual Sendgrid API Key.

![Updating env variable](https://dev-to-uploads.s3.amazonaws.com/i/m1vwn63x28qpe9ubyajk.png "Updating env variable")
*Updating env variable*

## Testing
Visit the Cloud Function’s Testing tab to test the function. Just click on ‘TEST THE FUNCTION’ to trigger the function.

![Testing the cloud function](https://dev-to-uploads.s3.amazonaws.com/i/4lwshrl7e4hcv805tr19.png "Testing the cloud function")
*Testing the cloud function*

Now let’s verify in our mailbox. Sometimes the email appears in spam, so be sure to check there if not received.

![Inbox](https://dev-to-uploads.s3.amazonaws.com/i/ur7i8ejv6w84x2j3474r.png "Inbox")
*Inbox*

You may wonder that why is ‘Sample Text’ not visible? It seems that  ‘text’ is a fallback in the event the email client does not support HTML emails.

Thus we can send emails programmatically in a serverless way. Note that because we worked in a library structure, we can extend this and have a similar setup for AWS Lambda.

All the code samples can be found in our git repository      {% github thefellowcoder/cloud-functions %}

Hope you found this tutorial helpful.

Let us know your thoughts in the comment section below.

[Original Article](https://thefellowcoder.com/blog/serverless-email-notifications-with-sendgrid/)