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.

  • url: will be gmail.com
  • email_to: is the recipient’s email address
  • subject: is email subject
  • msg: email body text
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.)

  • First “c” key is sent. This will open up the new email window.
  • email recipient address is sent as a whole string.
  • TAB key is sent. This will finalize the recipient address.
  • TAB key is sent again this will make the cursor jump to the next input area which is subject.
  • Subject string is sent
  • TAB key is sent again so the cursor jumps to the email body then the message string is sent
  • Finally TAB key is sent one last time and cursor moves above the Send button then the ENTER key is sent to send the email.
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)

That’s about it here is the full code on HolyPython Github Repository.

  • You can also combine this code with Task Scheduler either manually or automatically with coding.
  • Finally, it would also be a great Python learning project suitable for a Python GUI application.
  • You can write something to program multiple emails and have them sent at a convenient time.
  • You can also add a couple of extra TABs and sendkeys to add attachment option. This can be managed with an if statement so that if there is no attachment path defined code doesn’t take those extra steps. With SendKey operations it’s really crucial to get the exact key order right but it’s something you can experiment with until it’s perfect.
We also have a similar article for Twitter: How to automate tweets with Python.

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

Recommended Posts