Python email validation

I am interested in initiating a specific action when receiving email from a specific address with a specific subject. To be able to do this, I need to implement monitoring of my mailbox, checking each incoming mail (in particular, using gmail). What is the easiest way to do this?

+48
python email
Aug 04 '09 at 3:15
source share
7 answers

Gmail provides connectivity through POP, which you can enable in the gmail settings panel. Python can easily connect to POP:

import poplib from email import parser pop_conn = poplib.POP3_SSL('pop.gmail.com') pop_conn.user('username') pop_conn.pass_('password') #Get messages from server: messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] # Concat message pieces: messages = ["\n".join(mssg[1]) for mssg in messages] #Parse message intom an email object: messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: print message['subject'] pop_conn.quit() 

You just need to run this script as a cron job. Not sure which platform you are running on, so YMMV on how to do this.

+68
Aug 05 '09 at 4:04
source share

Gmail provides an atom feed for new email messages. You should be able to track this through authentication with py cURL (or some other network library) and pull out the feed. Running a GET request for each new message should mean it is read, so you won’t need to keep track of which letters you read.

+18
Aug 04 '09 at 3:18
source share

Until I was specific to Python, I always loved procmail , wherever I could install it ...!

Just use as some of your action lines for the conditions of your choice | pathtoyourscript | pathtoyourscript (a vertical AKA string followed by a script that you want to execute in these cases), and your mail will be transmitted through channels according to your conditions, to the script of your choice so that it does whatever it wants - it's hard to think about a more general approach to "initiate actions of your choice after receiving letters that meet your specific conditions!" Of course, there are no limits on how many conditions you can check, how many action lines a single condition can cause (just attach all the action lines you want in brackets { } ), etc. etc.

+8
Aug 04 '09 at 4:17
source share

People seem to be pumped up about Lamson:

https://github.com/zedshaw/lamson

This is an SMTP server written entirely in Python. I'm sure you could use this to do whatever you need - just forward Gmail messages to this SMTP server, and then do what you want.

However, I think it might be easiest to follow the ATOM recommendation above.

EDIT: Lamson was abandoned

+4
Aug 04 '09 at 17:51
source share

I found a pretty good snippet when I wanted to do the same (and the example uses gmail). Also check out Google search results .

+1
Aug 04 '09 at 3:18
source share

I recently solved this problem with procmail and python

Read the documentation for procmail. You can tell him to send all incoming letters in a python script like this to a special procmail configuration file

 :0: | ./scripts/ppm_processor.py 

Python has an affordable email package that can do anything you might want to do with email. Read the following ...

 from email.generator import Generator from email import Message from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.mime.multipart import MIMEMultipart 
0
Aug 04 '09 at 5:49
source share
 print('please enter str') def email(n=str): if ('@' and '.') in n: z=n.find('@') c=n.find('.') if type(n[:z] and n[z:c] and n[c:])==str: print('valid email') else: print('Invalid email') else: print('Invalid email') 
-7
Dec 17 '13 at 11:09
source share



All Articles