I need some help interpreting this error

Chris Green cl at isbd.net
Wed Feb 17 10:40:27 EST 2021


I'm running this using Python 3.7 on a Linux system.

Most of the time (i.e. for a couple of days now) the program has been
satifactorily delivering mail messages, hundreds of them.  However one
mail message has provoked the following error:-

    chris at cheddar$ tail mail.err
    Traceback (most recent call last):
      File "/home/chris/.mutt/bin/filter.py", line 95, in <module>
        if sbstrip in msghdr["subject"]:
    TypeError: argument of type 'Header' is not iterable


But msghdr["subject"] is surely just a string isn't it?  Why is it 
complaining about something of type 'Header'?

As I said the program has been running happily for a couple of days 
with no errors, I guess it must be something strange in a mail that
has broken things - but what?

Full program listed below:-

#!/usr/bin/python3
#
#
# license    Apache v2 (http://www.apache.org/licenses/LICENSE-2.0) 
# author     Chris Green - chris at isbd.co.uk
# 
#
#
# Mail filtering script
#
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/"
#
#
# Set to log to mail.log in ~/tmp with name 'filter' and the
envelope/from
#
log = mailLib.initLog("filter")
#
#
# Initialise destination mailbox name to its default "In/default"
#
dest = indir + "default"
#
#
# Read the message from standard input and make a message object from it
#
msg = mailbox.MaildirMessage(sys.stdin.buffer.read())
#
#
# Extract various headers and the envelope/from
#
msghdr =  {}
msghdr["to"] = msg.get("To", "unknown").lower()
msghdr["subject"] = msg.get("Subject", "unknown")
msghdr["list-id"] = msg.get("List-Id", "unknown").lower()
msghdr["list-post"] = msg.get("List-Post", "unknown").lower()
msghdr["x-mailing-list"] = msg.get("X-Mailing-List", "unknown").lower()
msghdr["unknown"] = "unknown"
#
#
# See if there's a match in our filter file
#
f = open(filtfile, 'r')
for ln in f:                    # for each line in filter
    if ln[0] == '#':            # ignore comments
        continue
    #
    #
    # split the line into fields, shlex.split() does quoted strings, add a field
    # to create a dummy fourth field if there isn't one in the filter file
    #
    fld = shlex.split(ln)
    fld.append("XXXXYYYYZZZZ")
    #
    #
    # copy the fields into better named variables
    #
    nm = fld[0]                 # name/alias
    destdir = fld[1] + "/"      # the destination directory
    header = fld[2]             # the header to find the match in
    address = fld[3].lower()    # list address to match
    sbstrip = '[' + fld[4] + ']'        # string to strip out of subject
    #
    #
    # Does the address in the header match this entry
    #
    if (address in msghdr[header]):
        #
        #
        # set the destination directory
        #
        dest = mldir + destdir + nm
        #
        #
        # Strip out list name (4th field) from subject if it's there
        #
        if sbstrip in msghdr["subject"]:
            msg.replace_header("Subject", msghdr["subject"].replace(sbstrip, ''))
        #
        #
        # we've found a match so assume we won't get another
        #
        break
#
#
# deliver the message
#
mailLib.deliverMdMsg(dest, msg, log)

-- 
Chris Green
·


More information about the Python-list mailing list