Send Email Using yagmail in Python
Last Updated : 07 Oct, 2021
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. This helps the Web App to interact with Gmail without any issues.
Note: This makes Gmail accounts vulnerable to some unauthorized access, so to impose security in Gmail accounts, use OAuth2 credentials to get access rights.
Installation:
pip install yagmail
Register User Email ID:
By registering, we allow the yagmail to access our Gmail account in consent to send emails. There is a need for an SMTP client to provide the authentication to the client for sending an email.
yagmail.register("Sender's Gmail Username", "Sender's Gmail Password")
Connect to SMTP server:
To initiate a connection with the SMTP server using the SMTP client, use the command below.
yag = yagmail.SMTP("[email protected]")
Adding Content and Delivering:
- In 1st argument in send() function, pass the receiver's email address.
- Then in the 2nd one, pass the Subject of the Mail your sender is sending.
- Now in the 3rd one, pass the Content of Mail i.e text or media.
yag.send("[email protected]","Subject Of Mail","Content(Text, Media etc)")
Sending simple email:
Python3 # importing yagmail and its packages import yagmail # initiating connection with SMTP server yag = yagmail.SMTP("Sender's Email Address", "Sender's Email Address Password") # Adding Content and sending it yag.send("Receiver's Email Address", "Subject of Email to be send", "Content(Text,Media, etc. files) of Email to be send")
Sending Email With Multiple Attachments
Here we will send an email with multiple attachments. In attachments, attributes pass through the list of attachments that have to be sent to the receiver.
Syntax: yag.send("[email protected]","Subject Of Mail","Content Of Mail", attachments= ['Attachment1.png',' Attachment2.png',' Attachment3.png'])
Code:
Python3 # importing yagmail and its packages import yagmail # initiating connection with SMTP server yag = yagmail.SMTP("Sender's Email Address", "Sender's Email Address Password") # Adding multiple attachments and mailing them yag.send("[email protected]","Subject Of Mail","Content Of Mail", attachments=['Attachment1.png','Attachment2.png','Attachment3.png'])
Output:

Sending Emails to Multiple Recipients
In "to" argument in send() function, pass the list of multiple receivers email addresses.
Syntax: yag.send(to=["[email protected]","[email protected]","[email protected]"], "Subject Of Mail","Content Of Mail")
Code:
Python3 # importing yagmail and its packages import yagmail # initiating connection with SMTP server yag = yagmail.SMTP("Sender's Email Address", "Sender's Email Address Password") # Adding multiple recipents name in "to" argument yag.send(to=["[email protected]","[email protected]", "[email protected]"],"Subject Of Mail","Content Of Mail")
Output:

Sending Emails with CC and BCC Fields
In cc(carbon copy) pass the receiver2 email address and in the 3rd one i.e in bcc(blind carbon copy) pass the receiver3 email address..
Syntax: yag.send(to="[email protected]",cc="[email protected]", bcc="[email protected]","Subject Of Mail","Content Of Mail")
Code:
Python3 # importing yagmail and its packages import yagmail # initiating connection with SMTP server yag = yagmail.SMTP("Sender's Email Address", "Sender's Email Address Password") # Passing other recipients name to cc and bcc arguments yag.send(to = "[email protected]", cc = "[email protected]", bcc = "[email protected]","Subject Of Mail","Content Of Mail")
Output:

Send an HTML Email
Pass the Content of Mail inside HTML tags. So, the message will be formatted following the HTML syntax that you have given.
Syntax: yag.send("[email protected]","Subject Of Mail","<h2>Content Of Mail</h2>")
Code:
Python3 # importing yagmail and its packages import yagmail # initiating connection with SMTP server yag = yagmail.SMTP("Sender's Email Address", "Sender's Email Address Password") # Passing content inside HTML tags to content argument yag.send("[email protected]","Subject Of Mail", "<h2>Content Of Mail</h2>")
Output:
Similar Reads
Send Emails Using Python By using Python, you can send emails which can be a valuable skill for automation, communication, and data-driven processes. In this article, we will explore how to send mail from Gmail using Python. How can you send Emails using Python?Python offers a library to send emails- "SMTP" Library. "smtpli
4 min read
Reading and Writing YAML File in Python YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that is commonly used for configuration files and data exchange between languages with different data structures. YAML is preferred in many cases due to its simplicity and readability compared to other formats like JSO
3 min read
Sending Email using FastAPI Framework in Python Before jumping into the topic directly, let's have a small intro about the technologies we are going to use. As the name suggests, we will be using FastAPI, a Python language framework. FastAPI: FastAPI is a python framework to develop REST Apis. It is very easy to build, Â high performance, easy to
3 min read
Parse a YAML file in Python YAML is the abbreviation of Yet Another Markup Language or YAML ain't markup Language which is the data format used to exchange data. YAML can store only data and no commands. It is similar to the XML and JSON data formats. In this article, we will dive deep into the concept of parsing YAML files in
4 min read
How to Send Beautiful Emails in Python In this article we will send stylized emails using Python we will be using smtplib. The Python smtplib module defines an SMTP client session object which can be used to send mail to any recipient with an SMTP listener. To send emails to any legitimate email address on the internet, "smtplib" produce
3 min read