Hi Guys,

I am currently looking for a Python solution to a project I am working on. I am VERY new to Python so please go easy on me.

Currently, I have a server setup, only running Windows 7, and on that server, Thunderbird is constantly running checking for new emails that are sent from a server producing nightly builds of some software.

When the email is received by the w7 server, a script is run that fires off some actions.

My problem is, I need to scan the emails received by Thunderbird, and then fire off a script based on the contents of the email received, so the email needs to be scanned, and a variable passed from it to the scripts that are fired off.

I have had a look around on the net for any helpers, but as I am very new to this I'm confused beyond belief at the solutions. Can someone please break down a simple solution for me?

So far I have the following. It connects to the IMAP mail server which is what I require, but I then need to query the emails which I'm struggling with.

Any thoughts? I'm not quite sure I have the first search query correct.

It needs to get a number from the subject, which is only part of the subject as defined by "Build No: xxx"

It then needs to scan through the body of the email to check to see if the build was successful. Which is in the format "Win Build - Passed" or "Win Build - Failed"

Any thoughts?

#!/usr/bin/env python

import os, sys, imaplib, rfc822, re, StringIO

server =' 'myserver'

M = imaplib.IMAP4_SSL(server)
M.login(username, password)

#I want to search for some text in the subject
typ, data = M.search(None, '(UNSEEN SUBJECT "text")')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])

#If an email has the correct subject, I then want to query a line in the body, and if
#...it passes, run another python script.


CODE tags would help enormously to understand your code. Either select code first and click the button or (usually easier for me) click button and right after do paste.

You might want to read this highly regarded blog about the module imaplib: http://www.doughellmann.com/PyMOTW/imaplib

Edited 4 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.