Send Email with Attachments by Outlook Email – Python SMTP Tutorial

By | June 7, 2019

In this tutorial, we will discuss about how to send email to others with attachments by using our outlook email in python. To send plain text message email by outlook email, you can view this tutorial.

Send Email to Others by Outlook Email – Python SMTP Tutorial

If you want to send email with attachment, you should do by these steps.

Step 1. Set sender email and password

sender = "xxx@outlook.com"
password = 'xxxxxxxx'

Step 2. Set receivers

receivers = ['tt@163.com','yy@126.com']

Step 3. Set attachment file

file_name = "F:\\D17-1052.pdf"

Step 4. Set outlook smtp server host and port

server_host = 'smtp.office365.com'
server_port = 587

Step 5. Create email text content

#create MIMEMultipart object
main_msg = email.mime.multipart.MIMEMultipart()

#create a MIMEText object, it is the text content of email
text_msg = email.mime.text.MIMEText("this is a email text content")
#add MIMEText object to MIMEMultipart object
main_msg.attach(text_msg)

Step 6. Create MIMEBase object to add attachment

contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)

#read attachment content
data = open(file_name, 'rb')
file_msg = email.mime.base.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
#file_msg is content of attachment
email.encoders.encode_base64(file_msg)

#attachment header
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition',
                    'attachment', filename = basename)
#add attachment to MIMEMultipart object
main_msg.attach(file_msg)

Step 7. Set email format

main_msg['From'] = sender
main_msg['To'] = ", ".join(receivers)
main_msg['Subject'] = "This attachment sent from outlook"
main_msg['Date'] = email.utils.formatdate( )

#full content of email
fullText = main_msg.as_string()

Step 8. Send email

The full example code is here.

#!/usr/bin/python

import smtplib
import email.mime.multipart
import email.mime.text
import email.mime.base
import os

#set sender email and password
sender = "xxx@outlook.com"
password = 'xxxxxx'

#set receivers
receivers = ['ttt@163.com','ggg@126.com']

#set attachment file
file_name = "F:\\D17-1052.pdf"

#set outlook smtp server host and port
server_host = 'smtp.office365.com'
server_port = 587

#create email text content

#create MIMEMultipart object
main_msg = email.mime.multipart.MIMEMultipart()

#create a MIMEText object, it is the text content of email
text_msg = email.mime.text.MIMEText("this is a email text content")
#add MIMEText object to MIMEMultipart object
main_msg.attach(text_msg)

#create MIMEBase object
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)

#read attachment content
data = open(file_name, 'rb')
file_msg = email.mime.base.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
#file_msg is content of attachment
email.encoders.encode_base64(file_msg)

#attachment header
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition',
                    'attachment', filename = basename)
#add attachment to MIMEMultipart object
main_msg.attach(file_msg)

#set email format
main_msg['From'] = sender
main_msg['To'] = ", ".join(receivers)
main_msg['Subject'] = "This attachment sent from outlook"
main_msg['Date'] = email.utils.formatdate( )

#full content of email
fullText = main_msg.as_string()

#send email by outlook smtp
server = smtplib.SMTP(server_host, server_port)
try:
    server.ehlo()
    server.starttls()
    server.ehlo()    
    server.login(sender,password)
    
    server.sendmail(sender, receivers, fullText)
    print ("Successfully sent email")
except SMTPException:
    print ("Error: unable to send email")    
finally:
    server.quit()

The result is:

Notice:

1.If you wan to send more files, you should use more main_msg.attach(file_msg) .

For example, if you use

main_msg.attach(file_msg)
main_msg.attach(file_msg)

You will send two same files.

Leave a Reply