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

Chris Green cl at isbd.net
Sat Oct 13 11:15:36 EDT 2018


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! :-)

-- 
Chris Green
·



More information about the Python-list mailing list