How to send emails with just a few lines of code with Yagmail in Python

davidmm1707

David MM👨🏻‍💻

Posted on September 27, 2019

How to send emails with just a few lines of code with Yagmail in Python

Original post How to send emails with just a few lines of code with Yagmail in Python

Yagmail tutorial

On our last lesson, How to send beautiful emails with attachments using only Python, we built a script to send an HTML-based email with attachments.

If you are using a Gmail account, we can significantly simplify our code with Yagmail, an STMP client for Gmail.



Setting up everything

We are going to send emails using a Gmail account. Before anything, create a Gmail account (or use your own) and enable the "Less secure app access" in your account. You can do it here: https://myaccount.google.com/u/0/security?hl=en&pli=1

Less secure access

After that, you are set! Create an environment with Python (I use pipenv, so I create one with pipenv shell), install yagmail with _pip install yagmail_and you are ready to go!


Our basic script

When I said that Yagmail simplifies our work I wasn't kidding. Let me show it to you.

First, create a Python file. Mine is yagmail_sender.py.

Now, write the following:

import yagmail

sender_email = YOUR_EMAIL
receiver_email = RECEIVER_EMAIL
subject = "Check THIS out"
sender_password = input(f'Please, enter the password for {sender_email}:n')

yag = yagmail.SMTP(user=sender_email, password=sender_password)

contents = [
  "This is the first paragraph in our email",
  "As you can see, we can send a list of strings,",
  "being this our third one",
]

yag.send(receiver_email, subject, contents)
Enter fullscreen mode Exit fullscreen mode

This is so clear that it doesn't need explanation.

But in case it does:

  • As usual, we state our sender and receiver email. Also a subject and a password typed by the user.
  • Then, we create a yagmail instance with the user and the password to log in.
  • The last variable, contents, is a list of strings. This is the body of our email
  • Then we send to the receiver receiver_email with the subject subject the email with the body that is listed on contents.

That's it. That's all you need:


Sending an email to a list of people

Can you imagine how to send that email to a list of email address?

Replace the old lines with the new ones:

receiver_email = RECEIVER_EMAIL # Old line
receiver_emails = [RECEIVER_EMAIL_1, RECEIVER_EMAIL_2, RECEIVER_EMAIL_3] # new line

....
yag.send(receiver_email, subject, contents) # Old line
yag.send(receiver_emails, subject, contents) # New line
Enter fullscreen mode Exit fullscreen mode

Notice the change from singular to plural.

Time to run the code again:

Yagmail tutorial - multiple emails sent

Every receiver on the list has received the email!

Disclaimer: Even if it's a list, it behaves like a set in Python. Repeated emails won't be sent an email twice.


Adding attachments

Sending attachments is incredible easy too. I don't want people fighting on the comments, so in this one I'll send two pictures: One of cats and another of dogs.

Add the desired files on the same root of your program and add the absolute path. Here's my contents:

contents = [
  "This is the first paragraph in our email",
  "As you can see, we can send a list of strings,",
  "being this our third one",
  "C:\\Users\\Void\\Desktop\\Codi\\Python\\yagmail\\dodgs.jpg",
  "C:\\Users\\Void\\Desktop\\Codi\\Python\\yagmail\\cats.jpg"
]
Enter fullscreen mode Exit fullscreen mode

I'm using a Windows OS, so I escaped the route with the backslash ( '').

That's all we need. Seriously. Run the code and you'll have two files attached to the email:

In case the code fails (we lose the internet connection, our password is wrong, etc), let's wrap everything with a nice try/catch:

import yagmail


sender_email = YOUR_EMAIL
receiver_emails = [RECEIVER_EMAIL_1, RECEIVER_EMAIL_2, RECEIVER_EMAIL_3]
subject = "Check THIS out"
sender_password = input(f'Please, enter the password for {sender_email}:n')

try: 
  yag = yagmail.SMTP(user='smtpforletslearnabout@gmail.com', password=sender_password)

  contents = [
    "This is the first paragraph in our email",
    "As you can see, we can send a list of strings,",
    "being this our third one",
    "C:\\Users\\Void\\Desktop\\Codi\\Python\\yagmail\\dodgs.jpg",
    "C:\\Users\\Void\\Desktop\\Codi\\Python\\yagmail\\cats.jpg"
  ]

  yag.send(receiver_emails, subject, contents)

except Exception as e:
  print(f'Something went wrong!e{e}')
Enter fullscreen mode Exit fullscreen mode

Let's introduce a wrong password:

Nice, now we get a text informing us that something went wrong and the message.

If you didn't know, this message is the same we can get when using the smtplib: Yagmail is just a wrapper around that library that simplifies the code greatly, as you just saw.


Conclusion

Yagmail is a wrapper around the smtplib library that help us to write short, good code. The catch is that you can only use it with Gmail addresses.

But as everybody uses them, that won't be a problem, right?

In case you are still using Hotmail or other email service, you can still use the smtplib Python library. Learn how to do it here:

How to send beautiful emails with attachments (yes, cat pics too) using only Python

And yes, I know that after looking at the cats and dogs pics on the email attachment you want to see the whole picture, not the thumbnail. You deserved it:

Yagmail tutorial cats

Yagmail tutorial dogs


Yagmail package

Yagmail docs


My Youtube tutorial videos

Final code on Github

Reach to me on Twitter

Read more tutorials

💖 💪 🙅 🚩
davidmm1707
David MM👨🏻‍💻

Posted on September 27, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related