I only download my email once every four hours. Otherwise I check it too much. This works really well and my "need to check" email addiction has totally shut off.
For this to work on your own email, you want to set up a Gmail account with an address that no one knows, as well as access to a cron task scheduler. Cron is an application that runs tasks on a set schedule, so every minute or every year or every Monday at 5pm, etc. If you have Mac or Linux, and you're the type of person that keeps their computer on 24/7, you can run "cron jobs" from your local machine (you want to edit the
/etc/crontab file - here are some instructions). I use my web hosting provider's cron service, because it will run even when my laptop is shut down.
Here's the script:
import imaplib from datetime import datetime import email import smtplib import sys import os import random import base64 def main(): #check user if os.isatty(sys.stdin.fileno()): #i'm trying to get my email by cheating, not a cron print "Are you sure you want to do this?" ans = raw_input() if ans != "yes": return no_of_mults = 3 for i in range(no_of_mults): a = random.randint(1000,9999) b = random.randint(1000,9999) print "What is " + str(a) + " * " + str(b) + "?" ans = float(raw_input()) if ans != a*b: print "Sorry, incorrect answer." return imap_domain = "imap.gmail.com" imap_port = 993 imap_username = base64.b64decode('my_gmail_username_encoded') imap_password = base64.b64decode('my_gmail_pass_encoded') #smtp settings smtp_domain = "my_smtp_host.com" smtp_port = 587 smtp_username = "my_smtp_user" smtp_password = "my_smtp_pass" email_recipients = [[email protected]'] imap_server = imaplib.IMAP4_SSL(imap_domain, imap_port) imap_server.login(imap_username, imap_password) server = smtplib.SMTP(smtp_domain, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(smtp_username, smtp_password) imap_server.select('INBOX') status, email_ids = imap_server.search(None, '(UNSEEN)') recipient = email_recipients count = 0 for e in email_ids.split(' '): if e is not '': try: raw_msg = imap_server.fetch(e, '(RFC822)') msg = email.message_from_string(raw_msg) #modify reply-to so we preserve email address if not msg['Reply-To']: msg['Reply-To'] = msg['From'] result = server.sendmail(msg['From'], recipient, msg.as_string()) count += 1 except smtplib.SMTPRecipientsRefused, e: print "SMTPRecipientRefused" + str(e) continue except Exception, err: print err continue # so I can see how many messages were printed print "Sent " + str(count) + " messages." filename = "/home/kevinburke/webapps/b/sent.txt" with open(filename, 'w') as f: f.write(str(count)) print datetime.now() server.quit() if __name__ == "__main__": main()
Then in your cron file, you want to add the following line:
0 */4 * * * /usr/bin/python /path/to/email/file.py
/path/to/email/fileis the filepath to wherever you saved the script above. The 0 means run the script at the top of the hour, and the */4 means run it once every 4 hours.
Liked what you read? I am available for hire.