Python code to send an email with an attachment

0 votes
I'm trying to automate the sending mail using python. I was successful doing that but I would like to modify it by adding attachments. How do I do that?
Jun 11, 2019 in Python by Vipul
15,151 views

1 answer to this question.

0 votes

Hi @Vipul, try out this code. I've given the explanation through the comments.

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

from = "sender email address"

to = "receiver email address"

# instance of MIMEMultipart

data = MIMEMultipart()

# storing the senders email address  

data['From'] = from

# storing the receivers email address 

data['To'] = to

# storing the subject 

data['Subject'] = "Subject of the Mail"

# string to store the body of the mail

body = "Body-of-the-mail"

# attach the body with the msg instance

data.attach(MIMEText(body, 'plain'))

# open the file to be sent 

filename = "File-name-with-extension"

attachment = open("Path of the file", "rb")

# instance of MIMEBase and named as p

p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form

p.set_payload((attachment).read())

# encode into base64

encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# attach the instance 'p' to instance 'msg'

data.attach(p)

# creates SMTP session

s = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security

s.starttls()

# Authentication

s.login(from, "Password-of-the-sender")

# Converts the Multipart msg into a string

text = data.as_string()

# sending the mail

s.sendmail(from, to, text)

# terminating the session

s.quit()


Hope this helps!!

If you need to know more about Python, join Python online course today.

Thanks!

answered Jun 11, 2019 by Adil
how to export a google spreadsheet as pdf and attach that pdf in the mail using this API?

Hi, @Aditya,

I would suggest you follow these regarding your query: https://www.labnol.org/code/19869-email-google-spreadsheets-pdf

Related Questions In Python

+1 vote
1 answer

How to use GUI that comes with Python to test your code?

Hey @alex0809, When your testing a website ...READ MORE

answered Sep 24, 2018 in Python by Vardhan
• 13,150 points
945 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
1,649 views
0 votes
0 answers

can i send an email using python?

can you give me the exact code ...READ MORE

Apr 4, 2019 in Python by Waseem
• 4,540 points
577 views
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24, 2019 in Python by Anjali
1,129 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

Python code to send an email message from my gmail to many others

Hey @Varsha, you can try out the ...READ MORE

answered Jun 11, 2019 in Python by Travis
1,089 views
0 votes
1 answer

Python code to send an email message from 1 email id to another

Hi @Deb, try out the following code: import ...READ MORE

answered Jun 11, 2019 in Python by Picentra
1,048 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP