imap folder scanner

Sebastjan Trepca trepca at gmail.com
Fri Mar 24 06:21:28 EST 2006


A very simple example...

import imaplib
m = imap.IMAP4(<myserver ip or host>)
m.login(username,password)
m.select('myfolder')
status, data = m.search(None,'(SUBJECT "BIKES")')
assert status=='OK', "Error. Message: %s"%data
data = data[0] #you get your results in a list and search returns only
one result
assert data,"No results"
#cool, we have results, but IMAP's search command only returns IDs so
we have to fetch
#msgs now
status,senders = m.fetch(data.replace('
',','),'(BODY.PEEK[HEADER.FIELDS (FROM)])')
assert status=='OK', "Error. Message: %s"%data

Now you just have to parse the "senders" data. There are many examples
about sending emails with python, like this one:

def send_notice():
	import smtplib
	msg = 'we got your mail, indeed'
	from email.MIMEText import MIMEText
	mail = MIMEText(msg, 'plain', 'utf-8')
	mail['From'] =fro='from at example.com'
	mail['Subject'] = "Spam machine"
	mail['To'] = to = 'to at example.com'
	server = smtplib.SMTP('localhost')
	errors = server.sendmail(fro, to, mail.as_string())
	server.quit()

That other program should be very simple to make now.

Sebastjan

On 3/24/06, Kun <neurogasm at gmail.com> wrote:
> Hey guys, I would like to have a code in python (as simple as possible)
> to scan a specific folder in my mailbox and if the subject is equal to,
> say, 'BIKES', I would like to have the code automatically send the
> SENDER an email saying something like "We have received your Email".
> Furthermore, I would also like to somehow save the sender's email into a
> list which would be compiled by another python program into an html file
> that would show a list of email addresses whose subject matched 'BIKE'
>
> I know i am asking for a lot but since i am new to python, can someone
> help me out with this? Whether its tips or code, i'll be very excited to
> hear your answer.  Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list