How to send email or SMS messages from Databricks notebooks

Learn how to send an email from your Databricks notebook.

Written by Adam Pavlacka

Last published at: May 17th, 2022

You may need to send a notification to a set of recipients from a Databricks notebook. For example, you may want to send email based on matching business rules or based on a command’s success or failure. This article describes two approaches to sending email or SMS messages from a notebook. Both examples use Python notebooks:

Send email or SMS messages from a notebook using Amazon SNS

This approach requires that you have an Amazon Simple Notification Service (SNS) article to send the notification to. Recipients who are subscribed to the SNS article will receive the email or SMS notification.

Delete

Info

You may need to install boto3, a Python library for working with Amazon AWS:

%python

pip install boto3

Add the following Python commands to your notebook, replacing <sample values> with your own:

%python

# Import the boto3 client
import boto3

# Set the AWS region name, retrieve the access key & secret key from dbutils secrets.
# For information about how to store the credentials in a secret, see
# https://docs.databricks.com/user-guide/secrets/secrets.html
AWS_REGION = "<region-name>"
ACCESS_KEY = dbutils.secrets.get('<scope-name>','<access-key>')
SECRET_KEY = dbutils.secrets.get('<scope-name>','<secret-key>')
sender='<sender@email-domain.com>'

#Create the boto3 client with the region name, access key and secret keys.
client = boto3.client('sns',region_name=AWS_REGION,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)

# Add email subscribers
for email in list_of_emails:
    client.subscribe(
        articleArn=article_arn,
        Protocol='email',
        Endpoint=email  # <-- email address who'll receive an email.
    )

# Add phone subscribers
for number in list_of_phone_numbers:
    client.subscribe(
        articleArn=article_arn,
        Protocol='sms',
        Endpoint=number  # <-- phone numbers who'll receive SMS.
    )

# Send message to the SNS article using publish method.
response = client.publish(
articleArn='<article-arn-name>',
Message="Hello From Databricks..",
Subject='Email Notification from Databricks..',
MessageStructure='string'
)

Send email from a notebook using Amazon SES

This example notebook demonstrates how to send an email message that includes HTML content and a file attachment, using Amazon Simple Email Service (SES).

Example Send email notebook

Review the Send email notebook.


Was this article helpful?