Have you ever wanted to send an email with GMail using the Python programming language? In 2018, Al Sweigart, best-selling author of Automate the Boring Stuff with Python, created a package called EZGmail. You can also use Google’s own bindings to do this sort of thing, but it’s a lot more complicated than using EZGmail.
In this article, we will take a quick look at how to use this package.
Your first step is to install EZGmail using pip. Here’s how:
pip install ezgmail
Then go to https://developers.google.com/gmail/api/quickstart/python and click the Enable the Gmail API button. This will allow you to download a credentials.json file and also give you a client ID and client secret. You can use the latter credentials with Google’s Python API client and you can manage these credentials here if you need to.
Now copy the credentials file to the location that you plan on writing your code. You will then need to run Python in your terminal in the same location that your downloaded credentials file is located.
The next step is to run
ezgmail.init(). This will open up a web browser to Gmail where it will ask you to allow access to your application. If you grant access, EZGmail will download a tokens file so that it doesn’t need to have you reauthorize it every time you use it.
To verify everything is working correctly, you can run the following code:
>>> ezgmail.EMAIL_ADDRESS 'email@example.com'
This should cause your Gmail account name to print out.
Sending Email with EZGmail
You can use EZGmail for sending and reading your emails.
Let’s look at a simple example of sending an email:
>>> email = 'firstname.lastname@example.org' >>> subject = 'Test from EZGmail' >>> text = 'This is only a test' >>> ezgmail.send(email, subject, text)
This will send an email to the account you specify with the subject and text supplied. You can also pass in a list of attachments to send. Finally, EZGmail supports CC and BCC, although if you send to multiple addresses using them or the email field itself, the arguments only accept strings. What this means is that the email addresses will need to be comma delimited within the string rather than a list of email addresses.
You can also use EZGmail for reading your email. The best two methods are using the
Here are some examples:
>>> recent = ezgmail.recent() >>> unread = ezgmail.unread()
Each of these methods returns a list of
GmailThread objects. A
GmailThread has the following attributes:
You can loop over these lists and extract any or all of these items as you see fit.
There is also a convenience function that you can use to print out a summary of your email messages:
When you run this code, it may take EZGmail a while to download and parse the emails. However note that it will only download a maximum of 25 email’s worth of data by default. You can change the maximum to a higher number if you need to, but beware that there is a data quota limit.
The EZGmail package is pretty neat. This tutorial didn’t cover it, but you can also use EZGmail to search your email using the
search() function. Be sure to give the package a try or at least study the source code. Happy coding!
- How to Send Email with Python