Send mail with attachment from your Gmail account using Python (original) (raw)
Last Updated : 22 Oct, 2017
In last article, we have discussed the basics of sending a mail from a Gmail account without any subject as well as without any attachment. Today, we will learn how to send mail with attachment and subject using Python. Before moving on, it is highly recommended to learn how to send a simple mail using Python and learn the basics working of ‘smtplib’ library of Python.
If you have read the previous article, you have gained the knowledge how a session is created and how it works. Now, you need to learn to attach a file and subject to the mail. For that you need to import some native libraries of Python. From these libraries, you need to import the tools used in our programs.
Steps to send mail with Attachments from Gmail account:
- For adding an attachment, you need to import:
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.mime.base import MIMEBase
- from email import encoders
These are some libraries which will make our work simple. These are the native libraries and you dont need to import any external library for this.
- Firstly, create an instance of MIMEMultipart, namely “msg” to begin with.
- Mention the sender’s email id, receiver’s email id and the subject in the “From”, “To” and “Subject” key of the created instance “msg”.
- In a string, write the body of the message you want to send, namely body. Now, attach the body with the instance msg using attach function.
- Open the file you wish to attach in the “rb” mode. Then create an instance of MIMEBase with two parameters. First one is ‘_maintype’ amd the other one is ‘_subtype’. This is the base class for all the MIME-specific sub-classes of Message.
Note that ‘_maintype’ is the Content-Type major type (e.g. text or image), and ‘_subtype’ is the Content-Type minor type (e.g. plain or gif or other media). - set_payload is used to change the payload the encoded form. Encode it in encode_base64. And finally attach the file with the MIMEMultipart created instance msg.
After finishing up these steps, follow the instructions described in the previous article to create a session, secure it and check the authenticity and then after sending the mail, terminate the session.
import
smtplib
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
from
email.mime.base
import
MIMEBase
from
email
import
encoders
fromaddr
=
"EMAIL address of the sender"
toaddr
=
"EMAIL address of the receiver"
msg
=
MIMEMultipart()
msg[
'From'
]
=
fromaddr
msg[
'To'
]
=
toaddr
msg[
'Subject'
]
=
"Subject of the Mail"
body
=
"Body_of_the_mail"
msg.attach(MIMEText(body,
'plain'
))
filename
=
"File_name_with_extension"
attachment
=
open
(
"Path of the file"
,
"rb"
)
p
=
MIMEBase(
'application'
,
'octet-stream'
)
p.set_payload((attachment).read())
encoders.encode_base64(p)
p.add_header(
'Content-Disposition'
,
"attachment; filename= %s"
%
filename)
msg.attach(p)
s
=
smtplib.SMTP(
'smtp.gmail.com'
,
587
)
s.starttls()
s.login(fromaddr,
"Password_of_the_sender"
)
text
=
msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
Important Points:
- You can use loops to send mails to a number of people.
- This code is simple to implement. But it will not work if you have enabled 2-step verification on your gmail account. It is required to switch off the 2-step verification first.
- Using this method, Gmail will always put your mail in the primary section and the mails sent will not be Spam.
Similar Reads
- Automatic Birthday mail sending with Python Are you bored with sending birthday wishes to your friends or do you forget to send wishes to your friends or do you want to wish them at 12 AM but you always fall asleep? Why not automate this simple task by writing a Python script. The first thing we do is import six libraries: pandasdatetimesmtpl 3 min read
- Python | Fetch your gmail emails from a particular user If you are ever curious to know how we can fetch Gmail e-mails using Python then this article is for you.As we know Python is a multi-utility language which can be used to do a wide range of tasks. Fetching Gmail emails though is a tedious task but with Python, many things can be done if you are wel 5 min read
- How to read Emails from Gmail using Gmail API in Python ? In this article, we will see how to read Emails from your Gmail using Gmail API in Python. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script. So, let's go ahead and write a simple Python script to read emails. RequirementsPytho 6 min read
- Sending SMS from Python with Google Cloud Functions In this article, we are going to talk about how we are going to send SMS (Short Message Services) with the addition of the Powers of Google Cloud Functions. Combining it with Twilio which is a cloud communications platform that provides APIs for sending and receiving SMS messages, as well as other c 5 min read
- Automate Renaming and Organizing Files with Python In this article, we are going to know how to automate renaming and organizing  files with Python, hence, the article is divided into two sections: one teaches us how to organize files and the latter how to rename files. Here we will learn about how to rename and organize files in Python. We will use 5 min read
- Google Maps Selenium automation using Python Prerequisites: Browser Automation using Selenium Selenium is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS, and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. It can 4 min read
- Creating and Viewing HTML files with Python Python is one of the most versatile programming languages. It emphasizes code readability with extensive use of white space. It comes with the support of a vast collection of libraries which serve for various purposes, making our programming experience smoother and enjoyable. Python programs are us 3 min read
- Send Email Using yagmail in Python In this article, we are going to see how to send email using yagmail. yagmail(Yet Another Gmail) Â is a module in python which used to send emails using Python. This module is nothing, just a Gmail/SMTP(Simple Mail Transfer Protocol) client that removes the issues of sending emails through Python. Th 3 min read
- How to Convert your Emails to PDF through Email Itself? In today's fast-paced digital world, converting emails to PDF format can be incredibly useful for archiving important information, sharing documents securely, and keeping records for future reference. But did you know that you can convert your emails to PDFs directly through your email itself? In th 6 min read
- Run shell command from GUI using Python In this article, we are going to discuss how we can create a GUI window that will take Operating System commands as input and after executing them, displays the output in a pop-up window. Modules Required: PyAutoGui: It is a module in python which is used to automate the GUI and controls the mouse a 2 min read