Take the 2-minute tour ×

Preferably using Python or Java, I want to compose an email and save it into gmail drafts without user intervention,

share|improve this question
Anyone else looking into this, i have also found: libgmail.sourceforge.net But I haven't tested it yet. –  Ben Page Mar 21 '11 at 9:35
libgmail uses mechanize and doesn't work. Last commit was in 2009. –  chbrown Mar 3 '13 at 13:26

2 Answers 2

up vote 8 down vote accepted

Here's a Python script to access a Gmail account. First you need to generate an OAuth token. Download Google's xoauth.py module and run it. It will walk you through the steps. You'll get a url to obtain a verification code -- paste this into the script and it will spit out your token and secret:

% python xoauth.py --generate_oauth_token --user=youremail@gmail.com

Once you've obtained your token and secret, copy them into the Python script below. It uses xoauth.py to authenticate the IMAP client, connects to IMAP, constructs a message and drops it into the Drafts folder.

import email.message
import imaplib
import random
import time
import xoauth

MY_EMAIL = 'youremail@gmail.com'
MY_TOKEN = '<token>'
MY_SECRET = '<secret>'

# construct the oauth access token
nonce = str(random.randrange(2**64 - 1))
timestamp = str(int(time.time()))
consumer = xoauth.OAuthEntity('anonymous', 'anonymous')
access = xoauth.OAuthEntity(MY_TOKEN, MY_SECRET)
token = xoauth.GenerateXOauthString(
    consumer, access, MY_EMAIL, 'imap', MY_EMAIL, nonce, timestamp)

# connect to gmail's imap service.
imap = imaplib.IMAP4_SSL('imap.googlemail.com')
imap.debug = 4
imap.authenticate('XOAUTH', lambda x: token)

# create the message
msg = email.message.Message()
msg['Subject'] = 'subject of the message'
msg['From'] = MY_EMAIL
msg['To'] = MY_EMAIL
msg.set_payload('Body of the message')

# append the message to the drafts folder
now = imaplib.Time2Internaldate(time.time())
imap.append('[Gmail]/Drafts', '', now, str(msg))

share|improve this answer
You might also want to check out the oauth2 module for authentication with oauth. With that you can just do imap.authenticate(url, consumer, token) and don't need to worry about using xoauth. –  Acorn Mar 20 '11 at 1:45
Thanks for the example code samplebias, that is exactly what I need and I will probably copy it almost exactly if thats ok. Acorn, thanks for the suggestion of the oauth2 module, I will look into that. –  Ben Page Mar 21 '11 at 9:30
Yep, I figured best to give you something that works out of the box so you can make progress, then fine tune it later. Good luck! –  samplebias Mar 21 '11 at 13:18
Hey, thanks for your answer. But my problem is one step further.(stackoverflow.com/questions/24361257/…). What I want is to set labels to a draft using imaplib in python, could you give me an answer? @samplebias –  Judking Jun 23 at 8:26

second person to ask something similar today, Using your GMail inbox space?

you could try doing this using a python imap client: imaplib quick imaplib+python+gmail returned: http://www.mattwarren.name/2008/08/2/python-imaplib-and-gmail/

a more complicated, but doable way, is using selenium/webdriver. you can automate almost anything.

share|improve this answer

Your Answer


