On 2014-05-05 20:58, Grant Edwards wrote: > On 2014-05-05, Ethan Furman <[email protected]> wrote: > > On 05/05/2014 12:51 PM, Grant Edwards wrote: > >> I'd like to do the polite thing and add a "Received:" header, > >> but I can't figure out how to get Python's email module to add > >> it in the correct place. It always ends up at the "bottom" of > >> the headers below From: To: etc. It's supposed to go at the > >> above all the Received: headers that where there when I received > >> it. > > > > I don't know that it matters, but which Python version? > > Sorry, should have mentioned it: 2.7.5
Looking at the stdlib source, it doesn't look like there's an easy
way to specify where it gets inserted. However, the source to
email.message.Message.add_header() is all of 9 lines of code, so it
wouldn't be too hard to subclass Message and twiddle self._headers as
you would any other list (i.e., using .insert() to specify an index).
It might look something like
class MyMessage(email.message.Message):
def insert_header(self, index, _name, _value, **_params):
parts = []
for k, v in _params.items():
if v is None:
parts.append(k.replace('_', '-'))
else:
parts.append(_formatparam(k.replace('_', '-'), v))
if _value is not None:
parts.insert(0, _value)
self._headers.insert(index, (_name, SEMISPACE.join(parts)))
You might still need to search for *where* you want to insert it, but
I'll leave that as an exercise to the reader. :-)
-tkc
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list
