Is this mailbox manipulation working by luck, or can't I understand my own code?

MRAB python at mrabarnett.plus.com
Sat Oct 13 13:06:31 EDT 2018


On 2018-10-13 16:15, Chris Green wrote:
> I use a Python script (called directly by '| <name of script>' in
> .forward) which routes incoming mail to various mailboxes according to
> the mailing list it's from (plus a few other criteria).  The first
> lines of the program are:-
> 
>      #!/usr/bin/python
>      #
>      #
>      # Mail filtering script
>      #
>      import email
>      import mailbox
>      import os
>      import sys
>      import time
>      import mailLib
>      import shlex
>      #
>      #
>      # Redirect any exceptions to a file
>      #
>      sys.stderr = open("/home/chris/tmp/mail.err", 'a')
>      #
>      #
>      # Some constants (i.e. configuration)
>      #
>      home = "/home/chris"
>      logfile = home + "/tmp/mail.log"
>      filtfile = home + "/.mutt/filter"
>      mldir = home + "/Mail/"
>      indir = mldir + "In/"
>      judir = mldir + "Ju/"
>      #
>      #
>      # Set to log to mail.log in ~/tmp with name 'filter' and the
>      envelope/from
>      #
>      log = mailLib.initLog("filter")
>      #
>      #
>      # Initialise destination mailbox name to empty
>      #
>      dest = ""
>      #
>      #
>      # Read the message from standard input and make a message object from
>      it
>      #
>      # msg = email.message_from_string(sys.stdin.read())
>      msg = mailbox.mboxMessage(sys.stdin.read())
>      #
>      #
>      # Extract the To:, Cc: and Subject: headers and the envelope/from
>      #
>      msgcc = msg.get("Cc", "unknown").lower()
>      msgto = msg.get("To", "unknown").lower()
>      msgsb = msg.get("Subject", "unknown")
>      msgfm = msg.get("From", "unknown").lower()
> 
> I then do various things according to the values in msgcc, msgto,
> msgsb and msgfm.  The program works well and I've been using it for
> quite a few years.
> 
> Now I want to do some minor changes and I can't for the life of me see
> how those lines with msg.get(... in them work.  The mailbox.mboxMessage()
> class doesn't have a get() method and the get() method for the parent
> class mailbox() doesn't seem to do what it appears to be doing here.
> 
> Can anyone suggest how this might be working?  What will those
> msg.get() calls return?  Will it simply be the whole message (there's
> always only one message fed into the program from .froward) or is
> there something going on that means it does work as I intended by
> getting the content of the individual header lines.
> 
> Looking at the rest of the code it *might* work OK even if all of
> msgcc, msgto, msgsb and msgfm contain the whole message.  It just
> wouldn't be working quite as I intended! :-)
> 
'mailbox.mboxMessage' is class 'mboxMessage' in file 'mailbox.py' in the 
stdlib.

In file 'mailbox.py' in the stdlib, class 'mboxMessage' inherits from 
'_mboxMMDFMessage', which inherits from '_mboxMMDFMessage', which 
inherits from 'Message', which inherits from 'email.message.Message' 
(class 'Message' from file 'email/message.py' in the stdlib.

Those msg.get() calls return is the contents (a string) of the relevant 
field in the message's header.

This:

     msg.get("Cc", "unknown").lower()

returns the contents of the "Cc" field if it exists, or the default 
value "unknown" if it doesn't exist.

That string is then converted to lowercase.



More information about the Python-list mailing list