Programmatically save a draft in the Gmail draft folder

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

+5
source share
2 answers

Here's a Python script to access your Gmail account. First you need to create an OAuth token. Download google xoauth.py and run it. He will lead you up the steps. You will receive a URL to receive a verification code - paste it into the script and it will spit out your token and secret:

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

, Python script . xoauth.py IMAP, IMAP, "".

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 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))

imap.logout()
+9

, - , GMail?

imap python: imaplib imaplib + python + gmail: http://www.mattwarren.name/2008/08/2/python-imaplib-and-gmail/

, , /webdriver. .

+1

All Articles