On 05/06/14 18:26, Grant Edwards wrote: > On 2014-05-06, Burak Arslan <burak.ars...@arskom.com.tr> wrote: >> On 05/06/14 12:47, Chris Angelico wrote: >>> On Tue, May 6, 2014 at 7:15 PM, alister >>> <alister.nospam.w...@ntlworld.com> wrote: >>>> On Mon, 05 May 2014 19:51:15 +0000, Grant Edwards wrote: >>>> >>>>> I'm working on a Python app that receives an e-mail message via SMTP, >>>>> does some trivial processing on it, and forwards it to another SMTP >>>>> server. >>>>> >>>>> 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. >>>> Is this required or just being polite? >>>> what I mean is does the standard state the headers must be in a >>>> particular order or can they appear anywhere, you may be spending time >>>> trying to resolve an issue that does not need fixing. >>> Yes, it's required. RFC 2821 [1] section 3.8.2 says "prepend". >> The rationale for "prepend" is to make it possible for MTAs to add >> their "Received:" headers to messages without having to parse them. >> >> So you're supposed to do the same: Just write your Received header, >> followed by '\r\n', followed by the rest of the message to the socket >> and you should be fine. > I need to check and manipulate other headers for other reasons, so I'm > using the email module for that. In order to keep things consistent > and easy to understand, I'd like to use the email module to prepend > the Received header as well. That keeps my application from having to > have any knowledge about e-mail message formatting. >
Seeing how discussion is still going on about this, I'd like to state once more what I said above in other words: You just need to do this: "Received: blah\r\n" + message.to_string() or better: socket.write("Received: blah\r\n") socket.write(message.to_string()) And again, this is not a hack, this is how it's supposed to work. Burak -- https://mail.python.org/mailman/listinfo/python-list