avatarJ3

Summary

The web content provides a comprehensive guide on automating email marketing tasks using Python, specifically demonstrating how to integrate Python with Outlook to send emails with attachments and Excel content.

Abstract

The article titled "Automate Your Email Marketing With Python" is the second episode in the #PurePythonSeries, which instructs readers on creating an Email Trigger System (ETS) in Python. It covers the use of Python's email package to construct sophisticated emails and the integration of Python with Microsoft Outlook for sending these emails. The guide includes practical examples, such as sending an Outlook test email and automating the distribution of area-specific reports with attachments using an Excel configuration file. It also addresses potential security prompts from Outlook and provides tips for streamlining the process, such as adjusting Trust Center settings and using f-strings for more intuitive code. The article concludes with a reminder to standardize processes before automation and offers additional resources, including Jupyter notebook links and related posts on Python programming topics.

Opinions

  • The author emphasizes the importance of having an anti-virus installed to change programmatic access settings in Outlook.
  • The use of Python's win32com.client module is recommended for interfacing with Outlook.
  • The article suggests that using f-strings can make the code more readable and intuitive when formatting email content.
  • The author, João Paulo Rodrigues de Lira, is thanked for his contributions, indicating a positive opinion of his work.
  • The article encourages reader interaction by asking them to applaud and subscribe if they find the content helpful, showing the author's desire for feedback and engagement.
  • The inclusion of a "Credits & References" section acknowledges the value of community and shared knowledge in learning and development.

Automate Your Email Marketing With Python

How To Create An Email Trigger System in Python — #PurePythonSeries — Episode #02

How does Python integrate with Outlook?

fig 0. PS Art: João Pedro Nogueira Oliveira
In this post, you’ll learn how to use the email package to send emails with MS Excel content and attachments using MS Outlook.

Python’s built-in email package allows you to structure more fancy emails, which can then be transferred with SMTP as you have done already.

01#Step — Test OUTLOOK

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '****@gmail.com'
mail.Subject = 'OUTLOOK test'
mail.Body = '''
Hi, 
This is a test for OUTLOOK.
If you receive this, 
YOU ARE READY TO GO!
'''
mail.Send()
Fig 1. software Outlook will ask for permission each time a program wants to send an email on your behalf

To avoid this message, Go To:

File>Options>Trust Center>Trust Center Settings>Programmatic Access.
The computer does have to have an anti-virus, so it will not allow a user to change the settings if it hadn't.
More info click here!
Fig 2. All is up and running!

02#Step — Test Email Trigger System (ETS) in Python

import pandas as pd
admin_df = pd.read_excel('Send_Emails_Index.xlsx')
admin_df.info()
Fig 3. This is the configuration file for Email Trigger System in Python o/

Now:

for i, email in enumerate(admin_df['E-mail']):
    admin = admin_df.loc[i, 'Administrator']
    area = admin_df.loc[i, 'Report']
    file = admin_df.loc[i, 'Files']
    
    mail = outlook.CreateItem(0)
    mail.To = email
    mail.Subject = '{} Report'.format(area)
    mail.Body = '''
    Dear {} Administrator, 
    
    Attached is the Report of {}, as requested.
    
    Any doubts I am available.
    
    Att.,
    
    JayThree.
    '''.format(admin, area)
    attachment  = r'C:\Users\giljr\Downloads\{}.xlsx'.format(file)
    mail.Attachments.Add(attachment)
mail.Send()
print("That's it! Thanks For reading! This post How do you automate emails in Python!")
That's it! Thanks For reading! This post How do you automate emails in Python!

I hope you enjoyed that lecture.

If you find this post helpful, please click the applause button and subscribe to the page for more articles like this one.

Until next time!

👉Jupiter notebook link :)

👉or here

👉git

Credits & References

Hashtag Treinamentos by João Paulo Rodrigues de Lira — Thank you dude!

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries —Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook—How To Create An Email Trigger System in Python (this one)

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

Note:
Alternatively you can use f-string, I find it even more intuitive. 
->Check it out here:)
for i, email in enumerate(admin_df['E-mail']):
    admin = admin_df.loc[i, 'Administrator']
    area = admin_df.loc[i, 'Report']
    file = admin_df.loc[i, 'Files']
    
    mail = outlook.CreateItem(0)
    mail.To = email
    mail.Subject = f'{area} Report'
    mail.Body = f'''
    Dear {admin} Administrator, 
    
    Attached is the Report of {area}, as requested.
    
    Any doubts I am available.
    
    Att.,
    
    JayThree.
    '''
    attachment  = rf'C:\Users\giljr\Downloads\{file}.xlsx'
    mail.Attachments.Add(attachment)mail.Send()

Standardize Before You Automate — J.P.Lira

Outlook
Python3
Send Email
Win32
Automation Tools
Recommended from ReadMedium