DVELP-mobilearrow-flex-shortarrow-flexarrow arrowsbinocularschartcogsflex-icongeargithubglobeheadphoneslightnings linkedinlock newsletter-chartDvelp_Newsletterplantscalestarget triangle-icon twitter

Automating Emails with Google Cloud Functions

September 23, 2019


Recently we found ourselves looking to automate some emails as part of our regular 360 degree feedback process. Our solution ended up using the SendGrid API, which we call from a Google Cloud Function. Here is the process we followed to get the very first iteration of this tool up and running:

  1. Write the function
  2. Deploy it using a Google Pub/Sub channel as it’s trigger
  3. Use Google Cloud Scheduler to write to the Pub/Sub channel at the desired times
  4. Test the trigger works

In the below we’ll walk you through these in some more detail.

1. Write the function

The first question to ask yourself is which language you want to use to write your function. Currently on GCP you have the following options:

  • Node (Versions 6, 8 and 10 are available)
  • Python 3.7
  • Go 1.11.6

We ended up writing our function in python. Even if you’ve never seen any python code before, you’ll see the below function is very straight forward. It uses the Sendgrid API client library to send a very simple email, from me to me.

import requests
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email

def sendgrid_email(event, context):
    message = Mail(
        to_emails="[email protected]",
        from_email="[email protected]",
        subject=f"REMINDER: DVELP Feedback",
    <p>Hi team,</p>
    <p>Please remember to provide your feedback.</p>
    <p>Muchas gracias,</p>

    sg = SendGridAPIClient()
    response = sg.send(message)

In order for this code to work, there are a few dependencies that are listed in the below requirements.txt file. This file is the equivalent of a package.json file in Node.js. When deploying a cloud function it is important to send this file along with your code, so that Google Cloud Platform knows how to configure your environment before running your function.


Last, but not least, you should add a .gcloudignore file to your directory. This works in exactly the same way as a .gitignore file: You can list the files you don’t want to deploy to Google and it will ignore them.


venv here is the virtual environment folder, which is python’s equivalent to node_modules. It contains a bunch of code that the cloud function wouldn’t use, so ignoring it speeds up deployment.

2. Deploy the function

If you don’t have a GCP account already, you can try it free here.

To deploy a Google Cloud Function you can download the gcloud cli tool from here.

Then, to log in to Google Cloud you enter the following command on the command line:

gcloud init

This will walk you through the sign in process for gcloud. The result is that you’re logged in on the right account and that the right Google Cloud Project is selected.

The command to deploy is:

gcloud functions deploy sendgrid_email 
    --runtime python37 
    --trigger-topic feedback-reminder 
    --set-env-vars SENDGRID_API_KEY=thisIsAnExampleSendgridApiKey

sendgrid_email is the name of our function. The --trigger-topic option tells it that I want any messages in the feedback-reminder Pub/Sub channel to trigger the function. And finally, --set-env-vars lets me tell GCP that my Sendgrid API Key is psst! it’s a secret!

Our function is deployed!

3. Schedule the trigger

To make sure the function gets called regularly, we need to set up a schedule on: https://console.cloud.google.com/cloudscheduler

Clicking “Create job” shows you:

Create job

Click Create. Now the feedback-reminder Pub/Sub channel will receive the message go! at 9am on the 14th of every month.

4. Test the trigger works

After clicking Create you see:

Create trigger

Clicking “Run now” will trigger the function and, if you’re me, you’ll have the below email sitting in your inbox:

The email

If this post got you thinking about opportunities for automating your communications and you could do with some help, please don’t hesitate to reach out. We’d be happy to advise you.


By using this website you agree to our cookie policy