Skip to content

How to receive emails by subscribing to a cloud Pub/Sub topic

This article is specific to the following platforms - BigQuery.

Overview

This article is a step by step guide to setting up Cloud Pub/Sub to receive emails.

Important Information

Users will need:

  • Access to the Google Cloud Platform (GCP) Console.
  • Users will need the following required GCP permissions:
    • pubsub.topics.list
    • serviceusage.services.list

Create a Topic

Follow the five steps below to first create a topic within the GCP Console.

1. Navigate to the GCP Console and log in.

2. Click the menu button to open the navigation menu.

3. Scroll down until you reach the heading "BIG DATA" and then first click on Pub/Sub and second click Topics.

4. Click CREATE TOPIC.

5. Specify a topic ID. In this example we have named our topic "docs-notification-topic".


Create a Cloud Function

Follow the steps below to next create a cloud function within the GCP Console.

6. In the GCP Console, click the menu button to open the navigation menu.

7. Scroll down to the heading "COMPUTE" and click Cloud Functions.

8. Click CREATE FUNCTION.

9. The Create function page requires users to configure the function.

Property Description
Name Specify a name for your new Cloud Function.
Memory allocated Select the computing memory option to allocate.
Trigger Select Cloud Pub/Sub from this dropdown menu.
Select a Cloud Pub/Sub topic Select the topic you created earlier in step 5.
Source code Select Inline editor.
Runtime Select Python 3.7.
Code Try the following:
import smtplib, ssl, os, base64, json
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Supply these via Environment Variables in the Cloud Function
SMTP_SERVER = os.environ.get('SMTP_SERVER')
SMTP_PORT = os.environ.get('SMTP_PORT')
SENDER_EMAIL = os.environ.get('SENDER_EMAIL')
SENDER_PASSWORD = os.environ.get('SENDER_PASSWORD')

def build_message(request):

receiver_email = request.get('receiver_email')
        subject = request.get('subject')
        text = MIMEText(request.get('message'), "plain")
        # html = MIMEText(html, 'html')

        message = MIMEMultipart("alternative")
        message["Subject"] = subject
        message["From"] = SENDER_EMAIL
        message["To"] = receiver_email
        message.attach(text)

        return message

    except Exception as e:
        # Print any error messages to stdout
        print(e)

def send_tls(event, context):

# print(event)
        request = json.loads(base64.b64decode(event['data']).decode('utf-8'))
        # print(request)
        ssl_context = ssl.create_default_context()
        ssl_context.check_hostname = False

        message = build_message(request=request)

        with smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT) as server:
            server.connect(host=SMTP_SERVER, port=SMTP_PORT) # seems redundant, but accommodates a bug in smtplib
            server.starttls(context=ssl_context)
            server.login(user=message.get('To'),password=SENDER_PASSWORD)
            server.sendmail(from_addr=message.get('From'), to_addrs=message.get('To'), msg=message.as_string())

    except Exception as e:
        # Print any error messages to stdout
        print(e)
Function to execute send_tls

The next two images provide further context to the above table.

Add Environment Variables

Click the ENVIRONMENT VARIABLES, NETWORKING, TIMEOUTS, AND MORE header.

Users will need to add four variables, detailed below. To add a variable, click + ADD VARIABLE.

Name Value
SMTP_SERVER Specify your simple mail transfer protocol (SMTP) server address.
SMTP_PORT Specify your simple mail transfer protocl (SMTP) port number.
SENDER_EMAIL Input the corresponding email address.
SENDER_PASSWORD Input the corresponding password.





Publishing Messages in Matillion ETL

In Matillion ETL, use the Cloud Pub/Sub component to publish messages to the Pub/Sub topic that you created previously.

Note

To successfully send email notifications, the message in the Cloud Pub/Sub component must be a JSON object that provides parameters that the Cloud Function expects. These parameters, which come as key-value dictionaries are:

  • receiver_email
  • subject
  • message

An example is provided below:

{
    "receiver_email":"alerts@matillion.com",
    "subject":"Test GCP Subject",
    "message":"This is a test message"
}


Contact Support

If you need help setting up Cloud Pub/Sub to receive emails, please contact support.