Python send GMAIL with attachment

Upasana | December 25, 2019 | 2 min read | 30 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. Send rich text multimedia email in Python
  2. Connect to MySQL with Python 3.x and get Pandas Dataframe
  3. Google Data Scientist interview questions with answers
  4. Python coding challenges for interviews
  5. Flask Interview Questions
  6. Connect to Postgresql with Python 3.x and get Pandas Dataframe
  7. Top 100 interview questions on Data Science & Machine Learning

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.