Table of Contents

    How To Automate Your Email Efforts With Python

    Email

    Written By Jason Rowse - Digital Marketing Expert

    Updated: 09/05/2021

    Creating an automated workflow for generating dynamic emails on python isn’t an easy task. You’ll have to build the MIMEMultipart object, attach files, and connect to SMTP/IMAP. However, doing so can make you an expert on email automation in the long run. Read on to find out how to get started right away. 

    How Does It Work? 

    To use python for email automation, two libraries ‘email’ and ‘smtplib’ are used. Email helps you create the information on its own while ‘smtplib’ is how you send the data to the right person. Your message is held within the multipart object called multipurpose internet mail extensions or MIME.

    The four sub-classes of MIMEMultipart are as shown below.

    • MIMEText is the plain text within your message
    • MIMEImage permits inclusion of images 
    • MIMEAudio lets you add audio to the body 
    • MIMEApplication eases the tasks of adding apps to the content 

    Once all the parts above are created and attached, it’s time to work with ‘smtplib’. Here, SMTP refers to simple mail transfer protocol. 

    With SMTP, you’ll have to encrypt the data with transport layer security or TLS protocol. After creating a link with the server, the message you created using the MIMEMultipart object is sent. 

    That’s it.

    Building The Message With MIME 

    The first part of producing an e-message with Python is creating the MIME or multipurpose internet mail extensions multipart email. Keep in mind that the four subclasses of MIMEMultipart aren’t necessary for every message. 

    Put simply, you can use just MIMEText if you’ll only need text content. However, marketing messages typically also use MIMEImage and MIMEApplication.

    Here are a few tips to help you out: 

    • The right side of MIMEMultipart indicates the subclasses used and the left shows the properties of the e-message
    • Import the essential libraries into the initial five lines
    • Assign the MIMEMultipart object to a ‘msg’ variable to initialize it 
    • Attach plain text, picture (image.jpeg), and attachment (report.docx) to the message (msg)
    • Access/modify messages and part properties as you would a Python dictionary
    • Add metadata to MIMEApplication objects using the key- ‘content-disposition’

    Attaching The Required Files 

    The above step showed how to attach individual subclasses of MIME, but you can also add as many as you want. Take a look at how to do it via the attach-technique according to the sub-class of the object. 

    • Loop through a list consisting of the file paths to your picture files such as pic1.png and pic2.png
    • Load the above into ‘img-data’
    • Read the pictures as binary via ‘rb flag’ specification for every subclass loaded when you ‘open’ the pictures
    • Create the MIMEImage object and attach it to your ‘msg’ 

    Connecting To SMTP 

    The next step is to set up the transfer of your marketing information between servers with ‘smtplib’. If you know exactly where the e-message lives, setting up SMTP is effortless. 

    For starters, every e-message service provider has a port (TLS) number and server address. For example, Gmail’s port number is 587, and the server address is smtp.gmail.com. 

    To initialize the SMTP connection for Gmail, you’ll have to add ‘smtp = smtplib.SMTP( ‘smtp.gmail.com’ ,  ‘587’). Subsequently, convey the mode of communication with the server. 

    Next, send an ‘extended hello’ or EHLO command via ‘smtp.ehlo’ to let the server know you can use extended SMTP (ESMTP). After this, you’ll have to work on the TLS encryption via ‘smtp.starttls’. 

    Here’s how to do this:

    • Use smtp.login to access SMTP server
    • Send the message with smtp.sendemail
    • Close the connection via the smtp.quit  

    Connecting To IMAP 

    Given that python gives you the ‘impablib’ module, you can use the internet message access protocol. This indicates how to connect to an email service provider to reclaim the messages sent to your email address. 

    You can review and delete emails using ‘imapclient’ and ‘pyzmail’.  First off, find out the IMAP domain name of the service provider just like you did for SMTP. For example, initializing the IMAP connection for Gmail refers to the domain name- imap.gmail.com.

    You’ll need to do the following:

    • Use imapObj.login to access the IMAP server
    • Select the relevant folder to search the message
    • Use the search () method of the IMAPClient object via the appropriate search keywords
    • Use the fetch () method to access the original content 
    • Perform the required action
    • Use imapObj.logout to disconnect from the IMAP server 

    Bottom Line 

    Setting up email automation with python is easy with ‘smtpblib’ and ‘email’ libraries. All you must know is the functionality of MIMEText, MIMEImage, MIMEApplication, and MIMEAudio. 

    To start, create the message with the MIMEMultipart object and add the important files. Once you’ve added as many files as you want, connect to the SMTP and IMAP to complete the automation workflow for Python.