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

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


# encode into base64


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

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


# creates SMTP session

s = smtplib.SMTP('', 587)

# start TLS for security


# 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


Hope this helps!!

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


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:

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,190 points
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,780 points
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
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
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 2,400 views
0 votes
0 answers
+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,810 points
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
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