Site icon Mailtrap

How to Send an Email With Yagmail

Yagmail is a simple GMAIL/SMTP client that is created to remove the hassle out of sending emails. With the Python library, you can write a few lines of code to include email sending capabilities to your application and sidestep using the cumbersome traditional approaches. In this tutorial, we’re going to demonstrate how to use Yagmail for sending emails.

So, let’s get started…

Ready to deliver your emails?
Try Mailtrap for Free

Project Setup

Let’s begin by setting up the following requirements for this Yagmail in Python tutorial:

pip install yagmail

Then, we’re ready to go!

How to Send an Email with Yagmail

Here is the code for sending an email using Yagmail:

#importing the Yagmail library
import yagmail
try:
    #initializing the server connection
    yag = yagmail.SMTP(user='my_username@gmail.com', password='mypassword')
    #sending the email
    yag.send(to='recipient_username@gmail.com', subject='Testing Yagmail', contents='Hurray, it worked!')
    print("Email sent successfully")
except:
    print("Error, email was not sent")

It’s that simple!

Let’s explain what is happening in the code above:

Here is an example:

yag = yagmail.SMTP("my_username@gmail.com", oauth2_file="~/oauth2_creds.json")

If we run the code and check our Gmail account, we find the message in the inbox:

That’s the simple way for Yagmail usage in sending emails.

Sending Email with Multiple Attachments in Yagmail

To send an email with an attachment in Yagmail, simply specify the attachments keyword argument as a parameter in the send() function and provide the attachment’s absolute path.

Here is an example:

#importing the Yagmail library
import yagmail
try:
    #initializing the server connection
    yag = yagmail.SMTP(user=''my_username@gmail.com', password='password')
    #sending the email
    yag.send(to=' recipient_username@gmail.com', subject='Sending Attachment', contents='Please find the image attached', attachments='Desktop/File 1/image1.png')
    print("Email sent successfully")
except:
    print("Error, email was not sent")

If we run the code and check our Gmail account, we find the attachment in the inbox:

To send an email with multiple attachments, simply pass a list of the attachments to the attachments argument.

Here is an example:

yag.send(to='user1@gmail.com', subject='Sending Attachments', contents='Please find the images attached', attachments=['Desktop/File 1/image1.png','Desktop/File 1/gantt2.png','Desktop/File 1/gantt3.png'])

If we check our Gmail account, we find the attachments:

Sending Emails to Multiple Recipients

Yagmail in Python makes it easy to send an email to a group of people. To do that, simply pass a list of email addresses to the to argument.

Here is an example:

  yag.send(to=['user1@gmail.com', 'user2@gmail.com', 'user3@gmail.com'], subject='Greetings...', contents='How are you?')

Here is the output:

Sending Emails with CC and BCC Fields

To send an email with CC (carbon Copy) field and BCC (Blind Carbon Copy) field, simply specify cc and bcc keyword arguments as parameters in the send() function.

Here is a Yagmail usage example:

yag.send(to='user1@gmail.com', cc='user2@gmail.com', bcc='user3@gmail.com', subject='Greetings...', contents='How are you?')

Let’s see the results in our Gmail inbox:

How to Send an HTML Email

Furthermore, setting up Yagmail to send an HTML email message is easy; in the contents argument of Yagmail’s send() function, we can pass a valid HTML and the message will be formatted according to the provided HTML syntax.

Here is an example:

yag.send(to='user1@gmail.com', subject='Greetings...', contents='<h1>How are you?</h1>')

Here is how the message appears in our Gmail inbox:

Sending Emails in Python Without Using Yagmail

One drawback of Yagmail is that it can be used only with Gmail email addresses. So, if you are looking to send emails in Python without the use of Yagmail, we advise you to check out our in-depth tutorial on the topic.

In the tutorial, we mention the usage of third-party sending solutions. More specifically, Mailtrap Email API.

Mailtrap Email API is an end-to-end sending solution that can deliver emails from any application using its email API or SMTP service. With it, you reach recipient inboxes and monitor your deliverability with dashboards and 30-day email logs.

The Mailtrap Email API dashboards are designed to give a snapshot of your email deliverability by showing you vital stats for a selected period along with a percentile comparison to the previous period.

The email logs show all the emails sent from your account and their delivery history. 

Additionally, the weekly reports give a detailed preview of your stats, including opens, clicks, bounces, unsubscribes, and spam reports.

Sending with Mailtrap Email API can be initiated after you have created a Mailtrap account, added and verified a domain, and pasted the email sending code snippet into your project. 

For Python (and other programming languages), you can find the snippet under Sending Domains-> API and SMTP.

Take the snippet you are provided with, add it to your Python project, run it, and start sending emails without any issues.

Wrapping Up

Gmail is a versatile email service that is widely used across the world. With Yagmail, you can simplify the tasks of sending emails to Gmail users and take the functionalities of your application to the next level. 

Of course, in this tutorial, we just scratched the surface of what is possible with Yagmail usage. If you want to see more usage examples, you can check its documentation.

If, on the other hand,  you now think that Yagmail is not for you, you can try sending your emails in Python with the Mailtrap Email Delivery platform instead.

Exit mobile version