Example of a email processing to a file

C

chuck amadi

Hi sorry to try and get someone to do my job but im at logger heads at
the moment .

I have been assigned a duty to abstract from a mail acoount
(/var/spool/mail/Usersurvey)

The email messages are encapsulated in xml I want to parse the data into
a database.

I have been programming with UnixMailbox Module and been able to view
the contents but how do I get all the messages into one file.I know the
database part just this extraction bit.

I fsomeone has a snippet or example would be great as I got a tight
deadline .

Cheers all
 
M

Mario Teijeiro

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

chuck amadi wrote:

I have been programming with UnixMailbox Module and been able to view
the contents but how do I get all the messages into one file.I know the
database part just this extraction bit.

Is this what you want ?

messages is a results's of rfc822.Message(....) list.

def save2mbox(dir_backup,mbox,messages,compressed,expired):
""" Guarda la lista de mensajes
en el directorio dir_backup y
con nombre mbox
"""
# Verificamos que existe dir_backup
import os
if not os.path.exists(dir_backup):
os.makedirs(dir_backup)

YYYY,MM,DD,hh,mm,ss,ms,mms,e =gmtime(expired)
mymbox="%s-%d%.2d%.2d" % (mbox,YYYY,MM,DD)
if compressed:
import gzip
fw=gzip.GzipFile(dir_backup+'/'+mymbox+'.gz','ab',5)
else:
fw=open(dir_backup+'/'+mbox,'ab')
for msg in messages:
try:
fecha=rfc822.formatdate(mktime(msg.getdate('date')))
except:
fecha=rfc822.formatdate(mktime(gmtime(0)))
fw.write("From %s %s\r\n" % (msg.getaddr('from'
[-1],fecha))
fw.write(msg.fp.getvalue())
fw.write("\r\n")
fw.close()
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAtpn2MKsc+XeZcdwRAis7AJ0SxLhGzV+mXNQUeiTHYOOb8csVTACdHgau
hwMYBLTVPk2hqbw3ZAhd2SU=
=P3lZ
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top