{"id":25864,"date":"2021-12-16T09:20:49","date_gmt":"2021-12-16T03:50:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=25864"},"modified":"2021-12-16T09:20:49","modified_gmt":"2021-12-16T03:50:49","slug":"python-yagmail-module-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-yagmail-module-with-examples\/","title":{"rendered":"Python Yagmail Module with Examples"},"content":{"rendered":"

Yagmail Module:<\/strong><\/p>\n

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.<\/p>\n

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.<\/p>\n

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.<\/p>\n

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.<\/p>\n

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.<\/p>\n

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

Installation of Yagmail Module:<\/strong><\/p>\n

Before going into the python code, install the Yagmail module in your system as shown below:<\/p>\n

pip install yagmail<\/pre>\n

Output:<\/strong><\/p>\n

Collecting yagmail Downloading yagmail-0.14.260-py2.py3-none-any.whl (16 kB)\r\nCollecting premailer Downloading premailer-3.10.0-py2.py3-none-any.whl (19 kB)\r\nRequirement already satisfied: cachetools in \/usr\/local\/lib\/python3.7\/dist-\r\npackages (from premailer->yagmail) (4.2.4) Collecting cssutils Downloading \r\ncssutils-2.3.0-py3-none-any.whl (404 kB) |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| \r\n404 kB 10.6 MB\/s Requirement already satisfied: requests in \/usr\/local\/lib\/python\r\n3.7\/dist-packages (from premailer->yagmail) (2.23.0) Collecting cssselect \r\nDownloading cssselect-1.1.0-py2.py3-none-any.whl (16 kB) Requirement already \r\nsatisfied: lxml in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from premailer->yagmail) (4.2.6) Requirement already satisfied: \r\nimportlib-metadata in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from cssutils->premailer->yagmail) (4.8.2) Requirement already satisfied: \r\ntyping-extensions>=3.6.4 in \/usr\/local\/lib\/python3.7\/dist-packages (from \r\nimportlib-metadata->cssutils->premailer->yagmail) (3.10.0.2) Requirement \r\nalready satisfied: zipp>=0.5 in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from importlib-metadata->cssutils->premailer->yagmail) (3.6.0) Requirement \r\nalready satisfied: certifi>=2017.4.17 in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from requests->premailer->yagmail) (2021.10.8) Requirement already satisfied: \r\nidna<3,>=2.5 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests->premailer\r\n->yagmail) (2.10) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,\r\n>=1.21.1 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests->premailer->\r\nyagmail) (1.24.3) Requirement already satisfied: chardet<4,>=3.0.2 in \/usr\/local\r\n\/lib\/python3.7\/dist-packages (from requests->premailer->yagmail) (3.0.4) \r\nInstalling collected packages: cssutils, cssselect, premailer, yagmail \r\nSuccessfully installed cssselect-1.1.0 cssutils-2.3.0 premailer-3.10.0 \r\nyagmail-0.14.260<\/pre>\n

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.<\/p>\n

Syntax:<\/strong><\/p>\n

yagmail.register('username', 'password')<\/pre>\n

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.
\nNow that we’ve registered the user, we can establish a secure connection with the SMTP client.<\/p>\n

We can utilize the customizable command-line options listed below:<\/p>\n

yagmail.SMTP('username', 'receiver1', 'receiver2', 'subject', 'body')<\/pre>\n

Parameters:<\/strong><\/p>\n

username:<\/strong> The email address of the sender
\nreceiver:<\/strong> This contains the email address of the recipient\/receiver. We can enter several email addresses for the receiver here.
\nsubject:<\/strong> A brief headline for the email
\nbody:<\/strong> Email message content<\/p>\n

If we do not indicate the recipient’s email address, the email is sent to the sender’s address.<\/p>\n

We proceed with the delivery of the content to the receiver’s email address after the content is complete.<\/p>\n

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.<\/p>\n

yagmail.send(to = [receiver1, receiver2, etc], subject=subject, contents=body)<\/pre>\n

Yagmail Module with Examples in Python<\/h3>\n

 <\/p>\n

Method #1: Using Yagmail Module (Static Input)<\/h3>\n

Approach:<\/strong><\/p>\n