Add header at top with email.message

2018-09-14 Thread Thomas Schneider
Hi,

the EmailMessage class of email.message provides the methods
add_header() and __setitem__() to add a header to a message.
add_header() effectively calls __setitem__(), which does
`self._headers.append(self.policy.header_store_parse(name, val))`.  This
inserts the header at the bottom.

It is, however, sometimes desired to insert a new header at the top of
an (existing) message.  This API doesn’t directly allow this.  In my
opinion, add_header() should have a flag at_top=False or similar, so
that one can get this behaviour (it’ll be a bit difficult with
__setitem__).  What do you think about this?  Is there a feasible way to
do this and change the library?  Should I post it somewhere where the
devs can hear it and suggest that?

Thanks,
--qsx
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Add header at top with email.message

2018-09-17 Thread Thomas Schneider
Jason Friedman  writes:

> I suppose you already figured out that you can call __delitem__() to
> clear the headers and add them back in whatever order you like.

Well, this would mean saving all headers, deleting all, inserting my
own, and adding the saved original headers again.  Seems complicated.

> I'm interested in learning more about your use case.  Do you have a
> third party with fixed logic that requires the headers in a particular
> order?

Yes, RFC 5321, section 4.4[0] :)

> When an SMTP server receives a message for delivery or further
> processing, it MUST insert trace ("time stamp" or "Received")
> information at the beginning of the message content, as discussed in
> Section 4.1.1.4.

To trace the path a message went, those headers do need to be in a
particular order, or else they won’t make any sense.

[0]: https://tools.ietf.org/html/rfc5321#section-4.4
-- 
https://mail.python.org/mailman/listinfo/python-list