mail processing module?

Preston Landers prestonlanders at my-deja.com
Fri Dec 22 11:15:27 EST 2000


In article <m266kd45m0.fsf at freak.kaiserty.com>,
  Michael Haggerty <mhagger at alum.mit.edu> wrote:

> Is there a reason to put sending and retrieving into a single module?
> They seem easily factorable.

You're really going to like this: it's all methods of one object. You
see, my main design goal here was to make working with mail as simple as
possible.

> In fact maybe a better breakdown would be one module that represents
> an email message and allows data to be MIMEd into and out of it, one
> module for sending such messages via SMTP, one module for retrieving
> such messages via POP, and one for retrieving via IMAP.

All of these modules exist already, are widely used and well tested.  My
module uses them.  Why would I abstract each module up a level but keep
them separate?

> interfaces for the two retrieving modules should be as close as
> possible, and ideally the interface to the mail-sending routine should
> be generic enough that one could write a module with the same
> interface but that sends emails by writing them to, e.g., sendmail or
> qmail.

My maillib module unifies the interface to POP and IMAP using a
technique that hard core OOPers will find abhorent:

if self.is_pop:
   do_one_thing()
elif self.is_imap:
   do_another()

Currently, only sending with smtp is supported but I could easily put in
support for using a command line mail sender.  Also, you could subclass
the maillib class and reimplement the sending methods.

> (Some of this functionality might overlap with existing Python
> modules.)

Ya don't say? ;-)  I plan to use them.  If all goes well I should be
able to post some code (or a pointer to it) within a couple of weeks,
and everyone will have a chance to show me the error of my ways then.
;-)

Like I said, I am trying to:

a) reimplement as little as possible

b) make the interface for using the module as simple as possible

Here's an example of use (sorry if there are formatting problems):

 import maillib

 # make mail exception availible in this namespace
 MailError = maillib.MailError

 # set up a configuration dictionary with the recommended options

 config_dict = {

     'sendmail_from': 'Your Name <your at address.here>',
     'sendmail_smtphost': 'smtp.yourdomain.com',
     'sendmail_user_agent':  'Catchy Slogan Here',

     'getmail_method': 'imap',  # or 'pop'
     'getmail_user': 'uname',
     'getmail_passwd': 'pwd',
     'getmail_server': 'imap-server.yourdomain.com',
     'getmail_server_port': 143, # standard imap port, 110 is POP port

     }

 Debug = None  # set to 1 for more verbose output to stderr
 my_mail = maillib.maillib(config_dict, Debug)  # you now have a maillib
object

 ### SENDING MAIL

 mailto_list = ['destination at address.com']
 subject = 'Hello, maillib!'
 mail_body = 'Hello, world.'

 attachment_list = [] # no attachments, empty list or None

 ### ATTACHMENT 1

 description_1 = 'Picture of Tux the Penguin'
 filename_1 = 'tux.jpg'
 mimetype_1 = None # let the module guess from the filename, only works
with common types
 item_1 = open_file_handle.read()
 attachment_1 = (description_1, mimetype_1, filename_1, item_1)

 attachment_list.append(attachment_1)

 try:
    my_mail.send_mail(mailto_list, subject, mail_body, attachment_list)
 except MailError, msg:
    print 'There was a mail sending problem'
    print msg
    raise

 print 'Mail sent ok.'


 # RECIEVE EMAIL

 try:
    my_mail.getmail_login()
    Delete = 1 # delete the message after you fetch it
    raw_message = my_mail.fetch_first_msg(Delete)
    my_mail.do_cleanup()  # logs off mail server!
 except MailError, msg:
    print 'There was a problem retrieving the email.'
    print msg

 ### The raw unprocessed message is now in raw_message

 ### DECODING MESSAGES

 try:
   (header_dict, message_body, attachments_list) =
my_mail.decode_email(raw_message)
 except MailError, msg:
    print 'There was a problem decoding the email.'
    print msg

  print 'From: ', header_dict.get('From', 'unknown')
  print 'To: ', header_dict.get('To', 'unknown')
  print
  print message_body


--- Preston <pibble at yahoo dot com>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list