[Tutor] parse emails as they come in

linuxian iandsd pylinuxian at gmail.com
Wed Apr 2 12:20:41 CEST 2008


well, here is a piece of final script :

#!/usr/bin/python
#

import sys

b=[]
while 1:
 data = sys.stdin.readline()
 if data != '\n':
  b.append(data)
 else:
  break

for i in (0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16):
 b[i]=b[i].split(':')[1].strip()
 #print b[i]

b[1]=b[1].split(':')
b[1]=b[1][1]+b[1][2]+b[1][3].strip()
#print b[1][1]+b[1][2]+b[1][3].strip()

bb=",".join(b)
print bb

mysqlcmd='insert into webdata field1, field2, field3, field4, field5,
field6, field7 ..... field17 values (%s)' % bb

print mysqlcmd

END OF SCRIPT


the parsed file looks like this :

field1: xxxxx
field2: xxxxx
....
field17: xxxxx
empty line here for end of message


On Wed, Apr 2, 2008 at 9:50 AM, Steve Willoughby <steve at alchemy.com> wrote:

> linuxian iandsd wrote:
>
> > well, i don't know how to pipe the file to my script !!
> >
>
> It's how procmail works.  Presuming you looked up how to write
> a procmail rule to save the body of your mail into a file, you
> should also see right next to it the instructions for piping
> the message to a program.  (It also can be found by a quick
> Google search, for future reference if you want a quick answer.)
>
> You just put a "|" symbol in front of the script name.
>
> To save the message to a file, you'd say something like
>
> :0:
> *Subject:.*pattern to look for
> /home/me/temp_input_file$date$time
>
> To pipe it to your script, you'd say something like
>
> :0
> *Subject:.*pattern to look for
> |/home/me/scriptname
>
> For more information see procmailrc(5) and procmailex(5).
>
> Your Python script will see the message input on stdin.
>
>
>
> > On Wed, Apr 2, 2008 at 7:18 AM, Steve Willoughby <steve at alchemy.com>
> > wrote:
> >
> >  linuxian iandsd wrote:
> > >
> > > > ok - as i mentioned in my first email i use procmail to put THE BODY
> > > > of
> > > >
> > > all
> > >
> > > > incoming mail into a file (that is one per incoming email as i use
> > > > the
> > > > variable $date-$time in the name).
> > > >
> > > > now this file can contain only one email but it can also contain 2
> > > > or
> > > >
> > > more
> > >
> > > > (this happens if for example there is a dns problem in the internet,
> > > > so
> > > >
> > > mail
> > >
> > > > can't make it, but once internet recovers from the dns problem mail
> > > >
> > > rushes
> > >
> > > > in & we may have multiple messages per file. this is also true is i
> > > > do
> > > >
> > > this
> > >
> > > Using $date-$time is insufficient since I'll wager a dozen doughnuts
> > > that the resolution of $time isn't small enough compared to the speed
> > > messages can arrive.
> > >
> > > But as I tried to explain in my previous mail, this is a problem you
> > > don't have to solve.  By choosing to use procmail to dump a file with
> > > a non-unique name, you create a race condition you then have to deal
> > > with in your code.
> > >
> > > If, on the other hand, you use procmail to _filter_ the message
> > > through your script, this cannot possibly happen.  You'll get an
> > > invocation of your script per message every time.  If you have
> > > your script directly dump the data into MySQL you never need to
> > > write any disk files at all.
> > >
> > >
> > >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080402/54405d8e/attachment.htm 


More information about the Tutor mailing list