[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

Chris Green schreef op 13/10/2018 om 17:15:
> 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:-
 > ...
>      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()

> Can anyone suggest how this might be working?  What will those
> msg.get() calls return?

I think your question is answered by this quote from the documentation 

"*The conceptual model provided by an EmailMessage object is that of an 
ordered dictionary of headers coupled with a payload that represents the 
RFC 5322 body of the message, which might be a list of sub-EmailMessage 
objects*. *In addition to the normal dictionary methods for accessing 
the header names and values*, there are methods for accessing 
specialized information from the headers (for example the MIME content 
type), for operating on the payload, for generating a serialized version 
of the message, and for recursively walking over the object tree.

The EmailMessage dictionary-like interface is indexed by the header 
names, which must be ASCII values. The values of the dictionary are 
strings with some extra methods. Headers are stored and returned in 
case-preserving form, but field names are matched case-insensitively. 
Unlike a real dict, there is an ordering to the keys, and there can be 
duplicate keys. Additional methods are provided for working with headers 
that have duplicate keys."

I.e. email.message instances provide the get() method because it is one 
of the "normal dictionary methods" you can use to access the headers.

More specifically, get() is documented as 

"Return the value of the named header field. This is identical to 
__getitem__() except that optional failobj is returned if the named 
header is missing (failobj defaults to None)."

Watch out for headers with duplicate keys (like multiple Received: 
headers): use get_all() for those 

"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven