The script below will check your gmail IMAP account for emails that match specific criteria (e.g. new emails from a specific person with a specific subject) and download all attachments from those emails. It can be configured to run as a cron job if needed.
Note that this requires installation of python. It was tested on WNDR3700v2 with Backfire 10.03.1 final
Step 1: Install python on your router:
opkg update
opkg install python python-openssl
Step 2: Here is the script:
#!/usr/bin/env python
import datetime, string, shutil, getpass, imaplib, email, os
detach_dir = '/tmp'
M = imaplib.IMAP4_SSL('imap.gmail.com',993)
M.login('USER_NAME', 'USER_PASSWORD')
M.select()
typ, data = M.search(None, '(FROM "SOME_USER@gmail.com" SUBJECT "SOME_SUBJECT" UNSEEN)')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
email_body = data[0][1] # getting the mail content
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
#Check if any attachments at all
if mail.get_content_maintype() != 'multipart':
continue
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " ["+mail["From"]+"] :" + mail["Subject"]
# we use walk to create a generator so we can iterate on the parts and forget about the recursive headache
for part in mail.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# is this part an attachment ?
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
counter = 1
# if there is no filename, we create one with a counter to avoid duplicates
if not filename:
filename = 'part-%03d%s' % (counter, 'bin')
counter += 1
att_path = os.path.join(detach_dir, filename)
#Check if its already there
if not os.path.isfile(att_path) :
# finally write the stuff
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
#Mark the email as read
#M.store(num, '+FLAGS', '\\Seen')
M.close()
M.logout()
Credit goes to this guy: http://www.python-forum.org/pythonforum … mp;t=17848