Open In App

Send Email Using yagmail in Python

Last Updated : 07 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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(“Sender@gmail.com”)

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(“Receiver@gmail.com”,”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(“Receiver@gmail.com”,”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("Receiver@gmail.com","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=[“recipient1@gmail.com”,”recipient2@gmail.com”,”recipient3@gmail.com”], “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=["recipient1@gmail.com","recipient2@gmail.com",
             "recipient3@gmail.com"],"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=”Receiver1@gmail.com”,cc=”Receiver2@gmail.com”, bcc=”Receiver1@gmail.com”,”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 = "Receiver1@gmail.com", cc = "Receiver2@gmail.com",
         bcc = "Receiver1@gmail.com","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(“Receiver@gmail.com”,”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("Receiver@gmail.com","Subject Of Mail",
         "<h2>Content Of Mail</h2>")


Output:



Similar Reads

Send PDF File through Email using pdf-mail module
pdf_mail module is that library of Python which helps you to send pdf documents through your Gmail account. Installing Library This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. pip install pdf-mail Function of pdf_mail This module only comes with a singl
2 min read
How to Send Automated Email Messages in Python
In this article, we are going to see how to send automated email messages which involve delivering text messages, essential photos, and important files, among other things. in Python. We'll be using two libraries for this: email, and smtplib, as well as the MIMEMultipart object. This object has multiple subclasses; these subclasses will be used to
6 min read
Send SMS updates to mobile phone using python
If you are running any python script and want to send regular updates from your script to your mobile phone through SMS, you can use SinchSMS API to send SMS.Approach : Create an app on Sinch and get the key and secret of the app and use these credentials in the following script to send SMS to your mobile.Limitation of Sinch : If you don't have any
2 min read
Python | Send SMS using Twilio
As we know Python is a cool scripting language and can be used to write scripts to easify day-to-day task. Also, since python has large community support and lots of module/API available, it makes Python more versatile and popular among users. In this article, we will see how to use Twilio API to send SMS using Python. It will be a very quick and e
2 min read
Send message to Telegram user using Python
Have you ever wondered how people do automation on Telegram? You may know that Telegram has a big user base and so it is one of the preferred social media to read people. What good thing about Telegram is that it provides a bunch of API's methods, unlike Whatsapp which restricts such things. So in this post, we will be sharing how to send messages
3 min read
How to send SMS alert using Python and MSG91 API
In our college days we most often forget our daily classes, right? To keep track of classes every day, we can send a Notification (i.e, ) SMS Alert regards their classes to their mobile phones using Python. We need to use two functionalities: http module and MSG91 API for sending SMS. import http.client as ht conn = ht.HTTPSConnection(&quot;api.msg
3 min read
Send Text messages to any mobile number using Fast2SMS API in Python
This article is going to be about how can we send text messages using Python. We will be using Fast2SMS API to send messages. You don't need to install any Python package for this purpose. First, you need a Fast2SMS account. You can sign up for Fast2SMS from here. Now, go to Dev API option and copy the API Authorization Key. This API key is generat
2 min read
Send Direct Message On Instagram using Selenium in Python
In this article, we will learn how we can send a direct message to users on Instagram without any manual action. We will be using the selenium module to do this task. Requirements:Chrome Driver for Chrome Browser (https://chromedriver.chromium.org/) or Gecko Driver for Firefox(https://github.com/mozilla/geckodriver/releases)Selenium Package. To ins
2 min read
Send Chrome Notification Using Python
In this article, we are going to see how to send a Chrome notification from your PC to a phone or several devices in this article. We may send messages, links, and other content. For this, we'll use Python's notify2 module and its various capabilities, which will help us in delivering messages to many devices. notify2 is a Python module that is use
2 min read
Send SMS with REST Using Python
In this article, we are going to see how we can send SMS with REST using Python. The requests library can be used to make REST requests using Python to send SMS. Approach:You need to first create a REST API KEY for sending SMS using Python Script. We have used Fast2SMS for creating API KEY.You can go on the website and create an account after which
2 min read
Practice Tags :