Hi Thomas

First off, I was wrong about the type of header values as returned by
any of the accessors.  They're just str (or list of str in the case of
`.get_all`).  I guess Header is a write-only feature (necessary if you
want to use non-ASCII in the address headers), because it isn't useful
for parsing most headers.

You *can* get a non-str, namely `None`.  But you say you tried
`msg.get('subject', '')`, which should return `''` in case of an
absent subject.  So unless you've figured that out in the meantime,
you still have to deal with it.  I would suggest printing or logging
`type(msg)` and `type(msg['subject']`.

Thomas Ward via Mailman-Developers writes:

 > In later Python, there is a class of message called
 > `email.message.EmailMessage` which has a few extra things than just
 > `email.message.Message` (I think this is yielded by `email.parser`
 > objects). By knowing that the class is essentially a subclass with
 > some fixes of `email.message.Message` then the `iter_attachments()`
 > approach I was using to iterate over attachments in the email
 > message won't work because that is only part of
 > `email.message.EmailMessage`.

If it isn't an `EmailMessage` (it probably isn't, everything in the
email package defaults to `Message` although there may be a better
policy than `Compat32`) you can probably do

```
def enhanceMessage(msg):
    # check type here to avoid mayhem
    msg.is_attachment = email.message.MIMEPart.is_attachment
    msg._body_types = email.message.MIMEPart._body_types
    msg.is_attachments = email.message.MIMEPart.is_attachments
```

(I didn't check the code terribly carefully, so if you get
AttributeErrors, just keep adding them according to the pattern
above.)

Or you could use something like
`msg = message_from_bytes(msg.as_bytes(), _class=EmailMessage)`,
although that's expensive.

 > archiver function by Mailman helps. Because now I have to do the
 > Old School(TM) way of iterating over `msg.walk()` and then
 > determining if the message part has a content disposition and then
 > confirm if it's an attachment or not and then have fun with it.

If for efficiency reasons you want to do it directly, the code in
/path/to/pypkgs/email/message.py, class MIMEPart is only about 30
lines including a couple of auxiliary methods and data.  It should be
easy to adapt.


-- 
GNU Mailman consultant (installation, migration, customization)
Sirius Open Source    https://www.siriusopensource.com/
Software systems consulting in Europe, North America, and Japan
_______________________________________________
Mailman-Developers mailing list -- mailman-developers@python.org
To unsubscribe send an email to mailman-developers-le...@python.org
https://mail.python.org/mailman3/lists/mailman-developers.python.org/
Mailman FAQ: https://wiki.list.org/x/AgA3

Security Policy: https://wiki.list.org/x/QIA9

Reply via email to