ClickAider

Python mail forwarder

When I got my blackberry I needed a way to forward other email accounts into the blackberry account. Here’s a little python script that does just that. Nothing special, but it does show what a great language Python is for this sort of thing.


# This Python script polls an IMAP account for new mail and# forwards it to other accounts.

# philbo at mailblocks dot com

import imaplib, string, smtplib, time

#—————————————————————-# CUSTOMIZE THIS INFO FOR YOUR EMAIL ACCOUNT

imap_server = ‘imap.source.com’imap_username = ‘yourname’imap_password = ‘password’

fromaddr = ‘yourname@source.com’toaddrs = [’yourname@destination.com’]

# interval in seconds for how frequently we check for new mailcheck_interval = 60

# you might need to customize this information if your# SMTP server is different than your imap server

smtp_server = imap_serversmtp_username = imap_usernamesmtp_password = imap_password

# No changes required beyond this point#—————————————————————-

while 1:    try:        server = smtplib.SMTP(smtp_server)        server.set_debuglevel(1)        server.login(smtp_username, smtp_password)

        M = imaplib.IMAP4(imap_server)        M.login(imap_username, imap_password)        M.select()        typ, data = M.search(None, ‘NEW’)

        for num in string.split(data[0]):            typ, data = M.fetch(num, ‘(RFC822)’)            server.sendmail(fromaddr, toaddrs, data[0][1])

        M.logout()        server.quit()        time.sleep(60)    except:        pass

1 Comment so far
Leave a comment

Cheers


Leave a comment

(required)

(required)