Adding attachment to email through outlook python

When working with Python, there may be times when you need to send an email with an attachment using Outlook. This can be achieved in different ways, depending on your specific requirements and the libraries you have available. In this article, we will explore three different options for adding attachments to emails through Outlook using Python.

Option 1: Using the win32com.client library

The win32com.client library provides a way to interact with Microsoft Outlook through COM automation. To add an attachment to an email using this library, you can follow these steps:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")

# Create a new email
email = outlook.CreateItem(0)

# Set the recipient, subject, and body of the email
email.Subject = "Email with Attachment"
email.Body = "Please find the attached file."

# Add the attachment
attachment = email.Attachments.Add("path/to/file.txt")

# Send the email

This code snippet creates a new email using the win32com.client.Dispatch method, sets the recipient, subject, and body of the email, adds the attachment using the Attachments.Add method, and finally sends the email using the Send method.

Option 2: Using the smtplib and email libraries

If you prefer to use the smtplib and email libraries, you can achieve the same result with the following code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# Create a new email
email = MIMEMultipart()
email["From"] = ""
email["To"] = ""
email["Subject"] = "Email with Attachment"

# Add the body of the email
email.attach(MIMEText("Please find the attached file."))

# Add the attachment
attachment = open("path/to/file.txt", "rb")
part = MIMEBase("application", "octet-stream")
part.add_header("Content-Disposition", "attachment; filename=filename.txt")

# Send the email
smtp = smtplib.SMTP("", 587)
smtp.login("", "password")
smtp.sendmail("", "", email.as_string())

This code snippet creates a new email using the MIMEMultipart class, sets the sender, recipient, and subject of the email, adds the body and attachment using the MIMEText and MIMEBase classes, and finally sends the email using the smtplib.SMTP class.

Option 3: Using the Outlook REST API

If you have access to the Outlook REST API, you can also add attachments to emails using Python. Here is an example using the requests library:

import requests

# Set the endpoint URL
url = ""

# Set the headers
headers = {
    "Authorization": "Bearer ACCESS_TOKEN",
    "Content-Type": "application/json"

# Set the email parameters
params = {
    "Message": {
        "Subject": "Email with Attachment",
        "Body": {
            "ContentType": "Text",
            "Content": "Please find the attached file."
        "Attachments": [
                "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
                "Name": "filename.txt",
                "ContentBytes": "BASE64_ENCODED_CONTENT"
    "SaveToSentItems": "true"

# Send the request
response =, headers=headers, json=params)

# Check the response status code
if response.status_code == 202:
    print("Email sent successfully.")
    print("Failed to send email.")

This code snippet sets the endpoint URL and headers for the Outlook REST API, defines the email parameters including the subject, body, and attachment details, and sends the request using the method. The response status code is then checked to determine if the email was sent successfully.

After exploring these three options, it is clear that the best choice depends on your specific requirements and the libraries you have available. If you are already using the win32com.client library or have access to the Outlook REST API, those options may be more suitable. However, if you prefer to use the smtplib and email libraries, option 2 provides a reliable solution.

Rate this post

9 Responses

    1. Are you serious? APIs are the backbone of modern technology! They enable seamless integration, enhance functionality, and foster innovation. Option 2 might work in some cases, but dismissing APIs altogether is short-sighted. Embrace progress, my friend.

  1. Option 1 sounds cool, but Option 3 using Outlook REST API feels more modern and efficient. What do you guys think? 🤔

    1. I completely disagree! Option 2 is a disaster waiting to happen. Option 3, on the other hand, offers innovative solutions and potential benefits we cant ignore. Its worth exploring the hidden perks and taking a calculated risk. Lets think outside the box! 💡

Leave a Reply

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

Table of Contents