When one talks about Python, it’s difficult to overlook how far the functionalities spread and what you can achieve with just a few pieces of code. Today, sending and receiving emails is the most relevant thing, and Python’s email automation in Microsoft Outlook makes things handier.
Why spend endless moments compiling emails on an email client when you can let Python do it for you in a single instance? If you are intrigued, you must read on to see how you can send automated emails from Python using Microsoft Outlook.
Fulfilling a Few Prerequisites
Considering the two major platforms referenced in this guide, these are the prerequisites for enabling this simple yet helpful task. To get started, download and install the following:
- Microsoft Outlook: You must have the MS Outlook application installed and running on your system. You can use any email domain with Outlook, as it doesn’t have any restrictions regarding email configurations.
- win32com.client: This third-party library is essential to connect to your Microsoft applications. Since Microsoft Outlook is one such application, you need this library within Python to connect to the Outlook exchange server.
Microsoft Outlook is one of the oldest and most widely used email clients, which ranks in the list of most popular email providers after Gmail and Yahoo. In this guide, a Gmail address is configured within Outlook.
win32com.client is an integral aspect of this code, and you need a fully functional library to establish a connection between Microsoft Outlook and Python.
Check win32com.client Version
Before installing, you can check if win32com is already installed on your machine. Some IDE versions offer this package by default. You can cross-check if it is available with the following command.
python -m pip show pywin32
If you get an error ‘pip’ is not recognized as an internal or external command while running the above command, you can install pip on Windows, Mac, and Linux, to ensure your library installations are seamless.
After running the above command, if you receive a version number, you don’t need to install it again.
Subsequently, if you get the following error message, you need to install the win32com library on your system:
'pywin32' is not recognized as an internal or external command, operable program, or batch file.
Installing win32com Library
Open the prompt and type in the pip command to install the library from the terminal window.
python -m pip install pywin32
Follow the onscreen instructions to complete the process. You can use the –show command post-installation to verify whether win32com is successfully installed on your system.
python -m pip show pywin32
Sending Emails From Python Using Outlook
Since the prerequisites are taken care of, it’s time to start writing the code. To start with, you need to import the win32com.client library by using the import statement.
import win32com.client
You can now write code to connect Python and Microsoft’s email application, Outlook.
ol = win32com.client.Dispatch('Outlook.Application')
Where:
- ol: New variable to store the connection reference.
- win32com.client: Windows library to establish a connection between Python and Outlook.
- Dispatch: Function to create the connection.
- Outlook.Application: This can be replaced with any Microsoft application name, as required.
Next, it is necessary to define the dimensions of the new email message so that Python understands where the content needs to be updated.
olmailitem = 0x0
Where:
- olmailitem: New variable to store the dimensions.
- 0x0: Dimensions of the new email message in Python’s memory.
Python’s functions pop-open a new email item, as soon as you define the email body dimensions.
newmail = ol.CreateItem(olmailitem)
Where:
- newmail: New variable to store the new email reference.
- ol: Reference of the previously created connection between Python and Outlook.
- CreateItem(olmailitem): Command to create a new email draft.
Since every email is incomplete without a subject line, you can define it within the code so that Python adds it automatically before sending the email to the recipient. This will be visible to the recipient, so be careful how you define the case and the content.
newmail.Subject = 'Testing Mail'
Where:
- newmail: Variable to store the new mail item reference.
- Subject: This can vary, depending on what you wish to have as the subject for your email.
You can add the intended recipients within the To and CC keywords as follows:
newmail.To = '[email protected]'
newmail.CC = '[email protected]'
Where:
- To: Main recipient’s email address.
- CC: Copied email recipients.
With Python, you can send emails to multiple recipients. Add a semi-colon (;) separator between email IDs within the To/CC column.
Finally, once you define the subject and the recipients, you can add the email body to the new mail item before sending it to the recipients within the To and CC columns.
newmail.Body= 'Hello, this is a test email to showcase how to send emails from Python and Outlook.'
To add attachments to your email, you can use the following commands:
attach = 'C:\\Users\\admin\\Desktop\\Python\\Sample.xlsx'
newmail.Attachments.Add(attach)
As the email is ready to be sent, there are two options you can use. If you want a preview of your email before sending it to the recipients, you can use the Display() command as follows:
newmail.Display()
You can use the Send() command if you want to send the email without reviewing it beforehand.
newmail.Send()
Remember, you won’t see a preview of the email if you use the Send command directly. Use this command wisely if you are changing your email body content regularly. You can use the Python code in one go to import the library, establish the connection, and send the emails quickly.
Here’s the complete code:
import win32com.client
ol=win32com.client.Dispatch("outlook.application")
olmailitem=0x0
newmail=ol.CreateItem(olmailitem)
newmail.Subject= 'Testing Mail'
newmail.To='[email protected]'
newmail.CC='[email protected]'
newmail.Body= 'Hello, this is a test email to showcase how to send emails from Python and Outlook.'
# newmail.Attachments.Add(attach)
newmail.Send()
Benefits of Python Email Automation
Python email automation enables you to send emails directly from your Python interface without opening MS Outlook. If you are an avid user of Python and not too keen on opening an email client repeatedly to type out emails, you can let the programming language do it for you.
Why spend hours doing repetitive tasks, when you can send automated emails using Python with a few lines of code?