Mail tranfer between Outlook Web Access and Gmail

Pubblicato: 1 novembre 2008 in Python, Tecnico
Tag:, ,

I had the problem to forward the content of some mailbox only accessible through Outlook Web Access (no POP no IMAP access, only web) to a Gmail account. Inspired by iphone-pop3-outlook, in particular by the OutlookWebScraper class, I wrote a small python script that permanently forwards the emails from OWA to Gmail (under a specific label) keeping the original dates:

import time, re, email, urllib, urllib2
from imaplib import *

### OWA Section
username = 'OWAUser'
password = 'OWAPass'
realm = 'my.owa.realm'
top_url = 'https://my.owa.domain'
destination = 'https://my.owa.domain/exchange'

### Gmail section
imap_server = ''
imap_user = ''
imap_pass = 'gmailpass'
gmail_label = "From OWA"

if __name__ == "__main__":
                password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
                password_mgr.add_password(realm, top_url, username, password)
                handler = urllib2.HTTPBasicAuthHandler(password_mgr)
                opener = urllib2.build_opener(handler)
                response = urllib2.urlopen(destination)
                html =
                m ='(?i)<base href="(&#91;^"&#93;*)">', html)
                base_href =
                inbox = base_href + urllib.quote('Inbox') + '/?Cmd=contents'
                response = urllib2.urlopen(inbox)
                html =
                message_urls = re.findall(r'(?i)NAME=MsgID value="([^"]*)"', html)
                server = IMAP4_SSL(imap_server)
                server.login(imap_user, imap_pass)
# Creating the destination label, can be 'INBOX'
                for mex in message_urls:
                        msg_href = base_href + mex + '?Cmd=body'
                        req = urllib2.Request(msg_href, headers={'Translate': 'f'})
                        response = urllib2.urlopen(req)
                        body =
                        msg = email.message_from_string(body)
                        date =  msg.get_all('date', [])
# Appending to the IMAP Gmail mailbox keeping the dates
                        server.append(gmail_label, '', Time2Internaldate(time.mktime(email.utils.parsedate(date[0]))), body)
# Delete the old email
                        req = urllib2.Request(base_href + mex, urllib.urlencode({
                                'MsgId': mex,
                                'Cmd': 'delete',
                                'ReadForm': '1'}))
                        response = urllib2.urlopen(req)
        except urllib2.URLError, e:
                print e

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...