How to Send Emails in Gmail using Python | HolyPython.com (original) (raw)

We’ll check out an automation idea with Python in this tutorial. There are many different ways to send emails using Python. This is one of them.

By using the webbrowser library we can open up a gmail page and then using shortcuts and SendKeys method (with a shell connection) we can just do the whole think by sending keyboard key sequences from Python to Windows.

It can be used as is or it can also be developed further as well as merged with other programs to carry out different email sending tasks.

It can be very good for a little bit of Python practice and it’s also very entertaining to watch Python manipulate Windows functions and accomplish important tasks.

Let’s get to it.

Prep (Enabling Gmail Shortcuts)

Since, Gmail has a keyboard shortcuts feature, we can actually use Python Send Keys method from Win32com to automate certain tasks such as sending email(s).

For this script to be able to succeed, first you’ll need to Enable Gmail Shortcuts.

Here is a great Gmail tutorial for that. Basically you need to navigate to Gmail and do something like: Gmail>Settings>Show All Settings>Enable Shortcuts.

After that tutorial says that c is the shortcut for “composing new email“.

We have pretty much all we need to write a simple script.

Gmail Settings to enable Shortcuts

Sending email with Gmail in Python (SendKeys method)

import webbrowser
import time
import win32com.client

Establish a connection to Windows shell.

shell = win32com.client.Dispatch("WScript.Shell")

Let’s create some variables so they are easy to change later when you want to create email variations or change the recipient’s address.

Gmail, Python Sending email Demonstration w/ Send Keys method.

url="www.gmail.com"
email_to="*****@*****.com"
subject="Hello World!"
msg="Like a Swiss watch."

Let’s open gmail in the default browser. I’m adding a 9 second delay in case the page is slow to load. Speed of this process can show variation depending on the computer, internet connection, time of the day, extension, firewall etc.

However, it’s really important to get it right so give it some time with some margin. The shortcoming of this method is that if one thing goes wrong, key sequence will be messed up so everything has to be perfectly thought out.

webbrowser.open("url")
time.sleep(9)

Finally, we can start sending key values to Gmail. (Your Gmail shortcuts should be enabled, explained on top of this post.)

Also, note that there is a delay of 1 second between each step. This is to ensure keys are registered properly because SendKeys method is very very fast. If you send like 10 keys they will all be executed in miliseconds. But that’s not how Windows functions so we need to spread out the little delays.

shell.SendKeys("c", 0)
time.sleep(1)
shell.SendKeys("email_to", 0)
time.sleep(1)
shell.SendKeys("{TAB}", 0)
time.sleep(1)
shell.SendKeys("{TAB}", 0)
time.sleep(1)
shell.SendKeys("subject", 0)
time.sleep(1)
shell.SendKeys("{TAB}", 0)
time.sleep(1)
shell.SendKeys("msg", 0)
time.sleep(1)
shell.SendKeys("{TAB}", 0)
time.sleep(1)
shell.SendKeys("{ENTER}", 0)

Closing & Some Ideas (Attachment, scheduling, gui, multi-emails)

Please share this page if you find it useful and thanks for checking out HolyPython’s Python tutorials.