How to Send Emails to Users using SparkPost in Databricks

Notifying users when the pipeline ran successfully or whenever an issue has occurred is one of the key components of an ETL pipeline.

In this blog, we have covered a step-by-step guide on how to send emails to multiple recipients using SparkPost in Databricks.

Before you start with Databricks, you need a SparkPost API key to send the mail using SaprkPost.

You can get the key by configuring SparPost as an External SMTP Email Provider.

Visit the link provided below to learn how to do this:

https://auth0.com/docs/customize/email/smtp-email-providers/configure-sparkpost-as-external-smtp-email-provider

Now that you have the API key with you, let’s start the Databricks notebook.

Step 1: Open the Databricks notebook.

Step 2: Copy, paste and change the below code as per your requirement:

# Variable for Subject of the mail
sbj = "Email Check"
 
#Variable for Body of the Email
bdy = "This email is sent from Databricks"


#Function that sends the e-mail

from sparkpost import SparkPost
module = 'EML'

def eml(sbj, bdy):
  sp = SparkPost('Put the SparkPost API Key here', 'https://api.eu.sparkpost.com')

  response = sp.transmissions.send(
      use_sandbox=False,
      recipients=['Support@ization.in','TechSupport@ization.in'],
      text=bdy,
      from_email='blog@ization.in',
      subject=sbj
  )
  print(response)

# Build email from variables and send.
eml(sbj, bdy)  

Step 3: Run the Notebook.

Step 4: Check the email.

Step 5: You are done.

Ready to elevate your skills? Click here for my Python book and here for my Machine Learning book on Kindle.

Leave a comment

Create a website or blog at WordPress.com

Up ↑