Example of a email processing to a file

Mario Teijeiro emeteo at agujero-negro.escomposlinux.org
Thu May 27 21:46:24 EDT 2004


-----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-----



More information about the Python-list mailing list