Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Add "Received:" header to email msg in correct position? Date: Thu, 8 May 2014 13:28:48 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 42 Message-ID: References: <536A4BF7.6040000@arskom.com.tr> NNTP-Posting-Host: dsl.comtrol.com X-Trace: reader1.panix.com 1399555728 22604 64.122.56.22 (8 May 2014 13:28:48 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 8 May 2014 13:28:48 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:71097 On 2014-05-07, Ian Kelly wrote: > On May 7, 2014 9:13 AM, "Chris Angelico" wrote: >> >> On Thu, May 8, 2014 at 1:06 AM, Burak Arslan > wrote: >> > 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. >> >> Yes, that method does work... if you're taking it in and sending it >> right out again. But it means you have to hang onto two pieces of data >> - the message and the new Received header - until you write it to a >> file/socket. > > Alternatively, you could use a BytesIO to prepend the Received header to > the raw data *before* you parse it with the email module. That's probably what I ought to do. But subclassing Message and adding a prepend_header was just too easy, and it avoids having to have my app know anything about the format of an email message in general or a header in particular. For example, I don't know what characters (if any) need to be escaped or specially encoded in the contents of a header. I also don't remember off the top of my head what you're supposed to do with headers that get too long (I think you just stick in a \r\n followed by some whitespace and then continue the header, but I'd have to look that up and then test it). I did know how to insert a tuple of two strings at the beginning of a list. :) -- Grant Edwards grant.b.edwards Yow! All this time I've at been VIEWING a RUSSIAN gmail.com MIDGET SODOMIZE a HOUSECAT!