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.gmail.com'
imap_user = 'gmailuser@gmail.com'
imap_pass = 'gmailpass'
gmail_label = "From OWA"

if __name__ == "__main__":
        try:
                password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
                password_mgr.add_password(realm, top_url, username, password)
                handler = urllib2.HTTPBasicAuthHandler(password_mgr)
                opener = urllib2.build_opener(handler)
                urllib2.install_opener(opener)
                response = urllib2.urlopen(destination)
                html = response.read()
                m = re.search(r'(?i)<base href="(&#91;^"&#93;*)">', html)
                base_href = m.group(1)
                inbox = base_href + urllib.quote('Inbox') + '/?Cmd=contents'
                response = urllib2.urlopen(inbox)
                html = response.read()
                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'
                server.create(gmail_label)
                server.select(gmail_label)
                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 = response.read()
                        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:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. 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...