Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Send Email Using yagmail in Python
Next article icon

Send Email Using yagmail in Python

Last Updated : 07 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
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("[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:


Next Article
Send Email Using yagmail in Python

A

adityatri
Improve
Article Tags :
  • Misc
  • Python
  • python-utility
  • python-modules
Practice Tags :
  • Misc
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences