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 in Python by Vipul
40 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()
answered Jun 11 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
• 12,530 points
73 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
111 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
• 4,480 points
21 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
41 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 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 Jun 11 in Python by Travis
66 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 in Python by Picentra
31 views