Send Gmail message using Autohotkey and Python


The bulk of this is in python and you need python 2.7 installed probably. Then open a command window and install the dependencies…
py -m -2 easy_install pyOpenSSL
py -m -2 easy_install pycrypto
py -m -2 easy_install google-api-python-client

Tip- make sure you install the modules to the correct version of python hence the -m -2 for default version of python 2.

Maybe biggest dependency/note is that you need a working google apps account which is not the same thing as a regular gmail account! Google apps is other wise known as google apps for business and costs $5 a month CAD. You need to have admin access to your google apps account and you need to use your google apps email to send the messages. You could probably also use a similar template to access all of the google api :).

Steps to setup your google apps account…
-save the python script (.py) file from to a folder
-with any gmail or apps account that has created a developers console account… enable the gmail api under APIs and auth
-under Credentials click add credentials, service account
-click on your new service account credentials and click generate new p12 key
-save that key to your folder with
-edit create your own python script as below and replace the client_email with the email from your credentials page
-replace the privatekey_path with the full path including file name and extension of your .p12 key
-enter your use_as_email as your google apps email and the other fields as required!

AHK code
subject := "ahk subject"
message := "ahk message"
python := "C:\Python27\python.exe"
run %python% -i "C:\Users\user_name\Desktop\Gmail\service account example\" "%subject%" "%message%"
import sys
from gmail_service_account_api import Google_apps_mail


client_email = ""
privatekey_path = "C:\Users\user_name\Desktop\Gmail\Gmail-blabla.p12"
use_as_email = ''

gmail = Google_apps_mail(client_email,use_as_email,privatekey_path)

sender='email' #this email doesn't seem to do anything
message = gmail.CreateMessage(sender, to, subject, message_text)
gmail.SendMessage(user_id, message)

Any questions let me know!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s