Python Yagmail Module with Examples

Yagmail Module:

In today’s world, almost every business has an internet presence. That is, they have an online presence in order to increase sales and market reach.

The email address of clients is one of the most common data elements collected by websites. We are frequently required to sign up for websites/portals with our email addresses.

In our email inboxes, we receive adverts or even sales/offers. They do not type and send emails to all of their consumers by hand. This suggests that the process of sending emails through the portal/application is automated in some way.

This is where the Python Yagmail module comes in handy. We may send emails to consumers using the Python Yagmail module by integrating the email module with our apps.

It sends emails in an automated and user-friendly manner using simple Gmail SMTP clients. We only need to offer a few more facts, such as an email address, the text of the email, and so on.

This module can be integrated as part of any retail or online application/portal; this is the module’s best use case.

Installation of Yagmail Module:

Before going into the python code, install the Yagmail module in your system as shown below:

pip install yagmail

Output:

Collecting yagmail Downloading yagmail-0.14.260-py2.py3-none-any.whl (16 kB)
Collecting premailer Downloading premailer-3.10.0-py2.py3-none-any.whl (19 kB)
Requirement already satisfied: cachetools in /usr/local/lib/python3.7/dist-
packages (from premailer->yagmail) (4.2.4) Collecting cssutils Downloading 
cssutils-2.3.0-py3-none-any.whl (404 kB) |████████████████████████████████| 
404 kB 10.6 MB/s Requirement already satisfied: requests in /usr/local/lib/python
3.7/dist-packages (from premailer->yagmail) (2.23.0) Collecting cssselect 
Downloading cssselect-1.1.0-py2.py3-none-any.whl (16 kB) Requirement already 
satisfied: lxml in /usr/local/lib/python3.7/dist-packages 
(from premailer->yagmail) (4.2.6) Requirement already satisfied: 
importlib-metadata in /usr/local/lib/python3.7/dist-packages 
(from cssutils->premailer->yagmail) (4.8.2) Requirement already satisfied: 
typing-extensions>=3.6.4 in /usr/local/lib/python3.7/dist-packages (from 
importlib-metadata->cssutils->premailer->yagmail) (3.10.0.2) Requirement 
already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages 
(from importlib-metadata->cssutils->premailer->yagmail) (3.6.0) Requirement 
already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages 
(from requests->premailer->yagmail) (2021.10.8) Requirement already satisfied: 
idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->premailer
->yagmail) (2.10) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,
>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->premailer->
yagmail) (1.24.3) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local
/lib/python3.7/dist-packages (from requests->premailer->yagmail) (3.0.4) 
Installing collected packages: cssutils, cssselect, premailer, yagmail 
Successfully installed cssselect-1.1.0 cssutils-2.3.0 premailer-3.10.0 
yagmail-0.14.260

Import the yagmail module. Once imported, we’d need to give the Yagmail module an account to utilize for authentication and delivering emails to the recipient. That is, we create a user account for the module. By registering an email address, the module may quickly connect to the SMTP server and send emails.

Syntax:

yagmail.register('username', 'password')

If we do not want to include our sensitive data as a parameter, such as a password, we can create a.yagmail file and save your sensitive data in that file instead of exposing it directly as a parameter.
Now that we’ve registered the user, we can establish a secure connection with the SMTP client.

We can utilize the customizable command-line options listed below:

yagmail.SMTP('username', 'receiver1', 'receiver2', 'subject', 'body')

Parameters:

username: The email address of the sender
receiver: This contains the email address of the recipient/receiver. We can enter several email addresses for the receiver here.
subject: A brief headline for the email
body: Email message content

If we do not indicate the recipient’s email address, the email is sent to the sender’s address.

We proceed with the delivery of the content to the receiver’s email address after the content is complete.

Yagmail gives us the send() function for this purpose. Here, we pack and encapsulate all of the material, as well as the receiver’s information, as well as the subject and body line.

yagmail.send(to = [receiver1, receiver2, etc], subject=subject, contents=body)

Yagmail Module with Examples in Python

 

Method #1: Using Yagmail Module (Static Input)

Approach:

  • Import the yagmail module using the import keyword.
  • Pass the username, password, host as the arguments to the yagmail.SMTP() function.
  • Store it in a variable.
  • Give the receiver’s address as static input and store it in another variable.
  • Give the subject as static input and store it in another variable.
  • Give the body of the email as static input and store it in another variable.
  • Pass the given receiver’s address, subject, and body of the email as the arguments to the send() function.
  • Print some random text for acknowledgment.
  • The Exit of the Program.

Below is the implementation:

# Import the yagmail module using the import keyword.
import yagmail
# Pass the username, password, host as the arguments to the yagmail.SMTP() function.
# Store it in a variable.
source_mail = yagmail.SMTP(user='[email protected]',
                           password='userpassword', host='smtp.gmail.com')
# Give the receiver's address as static input and store it in another variable.
receiver_addrs = "[email protected]"
# Give the subject as static input and store it in another variable.
subjct = "Python Programs"
# Give the body of the email as static input and store it in another variable.
body_mail = ["hello welcome to Python Programs"]
# Pass the given receiver's address, subject, and body of the email as the arguments
# to the send() function.
source_mail.send(to=receiver_addrs, subject=subjct, contents=body_mail)
# Print some random text for acknowledgment.
print("The Email Sent Sucessfully")

Note:

Check that SMTP access is enabled for the email address you intend to use. 
Most email providers prohibit SMTP access by default to prevent unauthorized 
programs from abusing it.