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?
6 days ago in Python by Vipul
16 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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()
answered 6 days ago by Adil

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
• 10,990 points
45 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,710 points
70 views
0 votes
0 answers

can i send an email using python?

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

Apr 4 in Python by Waseem
• 2,670 points
16 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 in Python by Anjali
14 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
55 views
+4 votes
6 answers
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 6 days ago in Python by Travis
49 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 6 days ago in Python by Picentra
15 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.