smtp - python

Adam Tauno Williams awilliam at whitemice.org
Thu Jun 9 07:24:08 EDT 2011


On Wed, 2011-06-08 at 17:18 -0300, Josias L.G wrote:
> Hi for all,
> I'm very newbie in python and is very good language.
> I'm trying to adopt a example:
> import smtpd
> import asyncore
> server = smtpd.PureProxy(('127.0.0.1', 1025), ('mail', 25))
> asyncore.loop()
> I'm trying to copy the email that is send to another email in maildir format.  
> Here, i'm reading about the mailbox module, however, i don't know how 
>start that (get the email that was transferred and, trought mailbox
>module, save all mails in one file). 
> an someone indicate something to me read ?.. examples... texts and 

I don't know much about the "mailbox" module; the documentation looks
straight-forward enough, what exactly is the question?
<http://docs.python.org/library/mailbox.html>?

If you want to put all the messages in a single file use mbox.

import mailbox
mybox = mailbox.mbox('my.mbox', create=True)
mybox.lock()
for message in messages:
    mybox.add(message)
mybox.flush()
mybox.unlock()
mybox.close()


To read a message into a Message object from a stream/file -

from email               import message_from_file
message = message_from_file(stream)

The best way to serialize a Message to a stream seems to be

from email.generator import Generator
tmp = BLOBManager.ScratchFile() # Create a stream
g = Generator(tmp, mangle_from_=False, maxheaderlen=60)
g.flatten(message)
tmp.seek(0)

-- 
Adam Tauno Williams <awilliam at whitemice.org> LPIC-1, Novell CLA
<http://www.whitemiceconsulting.com>
OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba




More information about the Python-list mailing list