Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Sending Emails With Yagmail

In the past lessons you had to write a lot of boilerplate code to send emails the way you wanted. In this lesson, you’ll meet Yagmail, which is a Python library created to interact with Gmail accounts and making it a lot easier to send emails via Python by writing less code.

Note: Don’t forget to install it via pip install yagmail

00:00 By now you know how to use Python to send all sorts of email, which is really cool. Automating sending emails is a great way to use the language. While smtplib does most of the heavy lifting dealing with the SMTP protocol, you may have noticed that for each project we’ve had to repeat many lines of code over and over and they’re the same from project to project. To avoid this, there are some libraries out there that are configured to work specifically with each type of email provider. In this video, you’re going to learn how to use Yagmail, which is configured to work with Gmail.

00:33 If you’re using a different email provider, you may need to look up a library for that specific one. And if there isn’t one, maybe you can write one yourself!

00:41 So, because this doesn’t come with Python, before you can use it, you need to pip install yagmail.

00:53 There we go. Collected and installed, so I can close this out. All right. So, import yagmail. You don’t need any of the smtplib stuff here.

01:03 Set a receiver equal to where you want the email to go.

01:11 And for the body of the email, I’m just going to make this very simple, so just something like 'Hello from Yagmail!'. Well, you know what? Let’s send that Cookie picture as well.

01:25 We’re going to send an attachment with the email. Here’s where Yagmail really comes into play. You’re going to make a variable called yag, and you’re going to set this equal to yagmail.SMTP().

01:38 And we want this to come from the sender email, which in my case is 'thecatinthehacks'.

01:48 And now you can just call

01:52 .send() on yag. You want to send this to the receiver. You want the subject to be—let’s say something like 'Yagmail test with attachment'.

02:10 The contents set equal to the body. And for attachments let’s just send in filename. And that’s literally it. This yagmail, this line right here, is opening that connection, doing all the SSL encryption.

02:28 It knows where Gmail’s SMTP host is, what port to go to. You contain everything in one line of code, basically. And then when you call .send() on that, this is pretty similar to the other emails that we’ve sent except for attachments—you just have to pass in the filename.

02:45 It knows that you want to load up that file, convert it to base64, and attach it. So this is really fun to use. Let’s save this, open up your terminal, and try to run it!

03:00 Okay. You can see I didn’t even have to put in an input() for the password. It knows that it needs the password for this email account.

03:09 And as I’m typing, you see I don’t have to blur anything out, because it just keeps it as this password cursor here. It’ll give you the option if you want to save that, and I don’t so I’ll just put n. And that’s it!

03:23 Let’s hop over to the inbox and see if it made it! I’ll try refreshing here. Nothing’s popping up. I did get one in my test email though, so let’s see. What do we have here?

03:40 And, of course, it helps if you’d put in the correct email. I should be putting in 'abcnotifytest', not that, so let’s just hop back here real quick and make sure this works.

03:49 We’ll correct that, save it, try to run that again.

04:04 Okay. Jump back over here. Let’s go to the correct one. And look at that! It was sent, and there’s the attachment. So this is really cool. I mean, looking at this whole script here, it’s like 16 lines and that’s with some header stuff up here.

04:21 This is just a great library if you need to work with Gmail. Now obviously, you lose some of the control over how this connection is made—and maybe you do need that. But in most cases, if you’re just trying to send out, like, a quick and dirty email, libraries like Yagmail make it very easy to do so. And that’s it!

04:39 You know how to send all sorts of different types of emails, you know some libraries that make it easier. Now it’s up to you to find out where you can start using email in your own projects. So go out, have fun, but please don’t send out too many spam emails. Thanks for watching.

sion on April 25, 2019

Very helpful, very interesting and well presented. Thank you

Joe Tatusko RP Team on April 25, 2019

You’re welcome! I’m glad you enjoyed it :D

yraheem on May 1, 2019

Awesome!!! I did have issues with the Yagmail module. I had to specify my username and password in creating the yag object like; yag = yagmail.SMTP(username, password) because without it the python interpreter raised an exception; file “C:......\site-packages\yagmail\password.py, in handle_password, password= keyring.get_password(“yagmail”, user) once I added my username and password, things seems to align.

Marc Gehling on May 1, 2019

same error as @yraheem, solved with “pip install yagmail[all]”

charliem22 on May 28, 2019

Excellent tutorial! Very clearly presented and logically structured! This is a model of how on-line education can work. Thanks for taking the time to make this!! charlie

carykinsfather on Aug. 20, 2019

You can also just pip install keyring to get it working. I couldn’t get [all] to work as shown above.

mikesult on Feb. 26, 2020

Great tutorial Joe. I learned some really useful techniques regarding email. Thanks!

ioseluisdev on Sept. 27, 2020

Great course. It was very clear all the content.

Ghani on Nov. 2, 2020

Very good tutorial; thanks!

Vasanth Baskaran on Aug. 26, 2021

@Joe Thank you for this wonderful tutorial. I am already started implementing the techniques learned here on my work. It is saving my time a lot.

aniketbarphe on Sept. 22, 2021

Absolutely fantastic! Please can you suggest how to deal with “Automation of email using Outlook also?”

Yogi on Oct. 31, 2021

Very concise and objectively prepared tutorial - thank you so much for this great, helpful learning session.

ranjeet on March 8, 2022

Nice course to send email using Gmail. Very informative and helpful, definitely I am going to use in effective way. Thank you.

Become a Member to join the conversation.