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.
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:
Poetry new simple_email
Then enter that directory.
cd simple_email
Once you are in the project folder you can run:
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.
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.
import yagmail
Next, set:
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:
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:
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:
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:
python main.py
in the shell of the Poetry environment in the project directory.
And here's proof that the email got sent:
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.
Top comments (0)