How To Send Email With Python, Simply
Tags: python, tutorial, beginners, programming
Published: 2023-08-21 17:09:49

Hello! I'm working on a video where I will use Python to send myself emails to help me sleep better.

But in that video, I'd like to be able to assume that the audience knows how to send emails with Python. So this blog post, and the accompanying video, will show you a simple and programmer-friendly way to send email using gmail and Python.

{% youtube QK_d6jlvLlI %}

## Steps

To begin with you should set up a virtual environment using Poetry. Poetry is my favorite package and virtual environment manager for Python.

To set up a new directory with a new project manage by Poetry, execute:

```sh
Poetry new simple_email
```

Then enter that directory.

```sh
cd simple_email
```

Once you are in the project folder you can run:

```sh
Poetry add yagmail keyring
```

which will install the dependencies we need to work with gmail for this project.

Next I will walk you through the source code. In the video I used `vim`, but you may use any text editor.

```sh
vim main.py
```

We are writing code in `main.py`. The first step is to import `yagmail`, which we installed with Poetry in the previous step.

```py
import yagmail
```

Next, set:

```py
yag = yagmail.SMTP()
```

In this case we are calling the constructor for the class SMTP in the package yagmail, and saving the resulting object to a variable namged `yag`. That object will provide the functionality we need to send email via gmail.

Next, write:

```py
contents = [
    "This is some test email content. Hello email reader."
]
```
This sets up the variable contents with a list of a single string containing the content for the email.

Then the last step is to simply use:

```py
yag.send('receiver@email.com', 'test email', contents)
```

Here 'receiver@email.com' is the email address of the person who will receive the email, 'test email' is the subject line of the email, and `contents` go in the body of the email.

The next step is to go to your Google account and set up an application password. Google will warn you about using an app password because it's not the most modern or secure method, but it is convenient, and it's appropriate for sending email from your own local Python scripts. Although I will admit that I used a secondary, development email address in the example in the video.

The screen for setting up an application password looks like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fimnknjvnr3ddjs6zevz.png)

I selected mail because I want to send email and gave it a custom name, in my case "Python".

Now we need to configure yagmail so that it was access to your username and password in a secure way. We could simple write the username and password in the Python script, but that's not a secure practice, and it's best to cultivate good security habits from the start.

The process here is to spawn a Python interpreter within the Poetry environment shell. In that shell:

```
>>> import yagmail
>>> yagmail.register('sender@email.com', 'yourpassword')
```

This will save your password to your computers keychain utility. (I tested it, and it works on mac, but will someone please try it on a PC and let me know?)

There is another configuration step, which I honestly almost forgot about when I was making the video. We could specify the sender email address in the Python script, but it's more convenient, and perhaps more stylish, to do so in a `.yagmail` file in your home directory, like this:

```
cd ~
vim .yagmail
```

The contents of `.yagmail` are simply the email address to be used as the sender. Nothing more, nothing less.

Once we've done all of this setup, we can test by running:

```sh
python main.py
```

in the shell of the Poetry environment in the project directory.

And here's proof that the email got sent:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkjhybkapvbwydcuogtw.png)

## Conclusion

Thanks for reading (or watching) and see you next time, when I will talk about cron, in preparation for my video on improving sleep quality with Python.