How to send emails with just a few lines of code with Yagmail in Python
Tags: python, tutorial, learning
Published: 2019-09-27 05:24:43


Original post [How to send emails with just a few lines of code with Yagmail in Python](https://letslearnabout.net/tutorial/how-to-send-easily-emails-with-yagmail/ "Permalink to How to send emails with just a few lines of code with Yagmail in Python")

![Yagmail tutorial][1]

On our last lesson, [How to send beautiful emails with attachments using only Python][2], 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][3], an STMP client for Gmail.

* * *

{% youtube D4dX1pueV54 %}
* * *

### 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][4]

![Less secure access][5]

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)

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:

![][6]

* * *

###  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

Notice the change from singular to plural.

Time to run the code again:

![Yagmail tutorial - multiple emails sent][7]

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"
    ]

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:

![][8]

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}')

Let's introduce a wrong password:

![][9]

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][2]: 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][2]

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][10]

![Yagmail tutorial dogs][11]

* * *

[Yagmail package][16]

[Yagmail docs][17]

* * *

[My Youtube tutorial videos][12]

[Final code on Github][13]

[Reach to me on Twitter][14]

[Read more tutorials][15]

[1]: https://i1.wp.com/letslearnabout.net/wp-content/uploads/2019/09/image-30.png?resize=688%2C325&ssl=1
[2]: https://letslearnabout.net/tutorial/how-to-send-beautiful-emails-with-attachments-using-only-python/
[3]: https://pypi.org/project/yagmail/
[4]: https://myaccount.google.com/u/0/security?hl=en&pli=1
[5]: https://i2.wp.com/humberto.io/img/emails/less-secure.png?w=688&ssl=1
[6]: https://i0.wp.com/letslearnabout.net/wp-content/uploads/2019/09/image-38.png?w=688&ssl=1
[7]: https://i1.wp.com/letslearnabout.net/wp-content/uploads/2019/09/image-39.png?fit=688%2C141&ssl=1
[8]: https://i0.wp.com/letslearnabout.net/wp-content/uploads/2019/09/image-40.png?w=688&ssl=1
[9]: https://i2.wp.com/letslearnabout.net/wp-content/uploads/2019/09/image-41.png?fit=688%2C45&ssl=1
[10]: https://i1.wp.com/letslearnabout.net/wp-content/uploads/2019/09/cats.jpg?fit=688%2C387&ssl=1
[11]: https://i0.wp.com/letslearnabout.net/wp-content/uploads/2019/09/dogs.jpg?w=688&ssl=1
[12]: https://www.youtube.com/channel/UC9OLm6YFRzr4yjlw4xNWYvg?sub_confirmation=1
[13]: https://github.com/david1707/yagmail_sender
[14]: https://twitter.com/DavidMM1707
[15]: https://letslearnabout.net/category/tutorial/
[16]: https://pypi.org/project/yagmail/
[17]: https://buildmedia.readthedocs.org/media/pdf/yagmail/latest/yagmail.pdf