Sending Automated Emails with Python

Python is a great tool for automation in the cloud, but we need a way to report results back to us. This is where email can be a great tool.

We can utilize Python to draft and send emails, which is a great way to display results from an automated pipeline. We incorporated this into our Fantasy Basketball Optimization pipeline, where we sent ourselves a daily automated email with player recommendations based on the pipeline results.

This article explains the framework we used for this, including the basic setup and Python code. It’s actually a lot simpler than you think, so let’s get into it!

Email Setup

To start, you’ll need some sort of email account to use where you can feed the password to Python. In our case, we simply created a new Gmail account and saved the password as an environment variable on our desktop.

With our Gmail account setup, we started by defining a few variables with the to and from email address as well as retrieving the password for our new Gmail account:

import os 

# to and from user info 
to_email = "ADD_YOUR_TO_EMAIL@gmail.com" 
sender_email = "ADD_YOUR_FROM_EMAIL@gmail.com" 

# get the email password from an environment variable 
password = os.getenv("EMAIL_PASSWORD") 

Next, we’ll define the content of our email, including the subject and body. Note that with this method, we’ll just have a simple text string for the body of our email.

# define the subject 
email_subject = "Test Email from Python Script" 

# define the body 
email_body = f'''
Hi {to_email}, 

This is a test email successfully sent from a Python script. 

Congrats, it worked! 
''' 

We also need to setup an email server connection using the smtplib and ssl packages. Since we have all of our email credentials, this can be done pretty easily with just a few lines of code:

import smtplib 
import ssl 

# connection constants  
smtp_server = "smtp.gmail.com"
port = 465  # For SSL 

# setup the email connection 
context = ssl.create_default_context()
server = smtplib.SMTP_SSL(smtp_server, port, context = context) 
server.login(sender_email, password) 

With everything in place, there’s just one more chunk of code we need to draft and send our email:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText 

# create the message and add the details
msg = MIMEMultipart() 
msg['From'] = sender_email 
msg['To'] = to_email 
msg['Subject'] = email_subject 

# add the message to the email 
msg.attach(MIMEText(email_body, 'plain')) 

# send the email 
server.sendmail(sender_email, to_email, msg.as_string()) 

# close out the connection when we're done 
server.quit() 

As soon as you run the last chunk, the demo email should get sent to the target email address, and that’s all it takes!

Conclusion

While Python in great for automation, we still need a way to report results back to us. In this article, we saw that you can pretty easily utilize Python to draft and send emails, making it a great option for sharing pipeline results.

Feel free to visit our GitHub repo to download the full notebook of pipeline code and try it out yourself.

As always, we hope you learned something today, and we’ll see you next time!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top