Send email with attachment in Python

Carvia Tech | October 31, 2019 | 2 min read | 2 views


Here, we are going to learn how to send email with attachment in python. We will be using smtplib for email via gmail client.

Define sender & recipient

fromaddr = "xxx@gmail.com" (1)
toaddr = "yyy@gmail.com" (2)

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Resume for Data Scientist Profile" (3)
1 Email of sender
2 Email of recipient
3 Subject of Email

Define body of the email

body = "Please find attached my resume" (1)
msg.attach(MIMEText(body, 'plain'))
1 Body of the email

Attach the file

path = "xx/yyy/" (1)
filename = "resume.pdf" (2)
attachment = open(path + filename, "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)
1 Define path of file that we want to attach in the email
2 Define filename that we want to send as attachment in the email

Setup Server and send email

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "****password****")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Now the whole script for sending email will be like below :

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


fromaddr = "xxx@gmail.com"
toaddr = "yyy@gmail.com"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Resume for Data Scientist Profile"

body = "Hi, Please find attached my resume. Thanks, Carvia"
msg.attach(MIMEText(body, 'plain'))

path = "xx/yyy/"
filename = "resume.pdf"
attachment = open(path + filename, "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "***password***")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Since we are sending email via a gmail, google will not allow you to log in via smtplib because it has flagged this login as less secure.

So In order to allow us to send email via gmail, you will have to login to your gmail or sender’s gmail account and then visit this link :

When you will visit this link, you will see this

Allow less secure apps

Click on the slider to allow less secure apps. Once, this is ON we will be able to send email via gmail account.

Run the script and check if received email

Run the file and you shall have received email with attachment. That means we are good to go.

I hope this helped you. Thanks for reading the article.


Top articles in this category:
  1. Python Flask Interview Questions
  2. Python coding challenges for interviews
  3. Top 100 interview questions on Data Science & Machine Learning
  4. Google Data Scientist interview questions with answers
  5. Send rich text multimedia email in Python
  6. Sequence of Differences in Python
  7. Find extra long factorials in python



Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info