Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'receives': 0.04; 'socket': 0.07; 'rfc': 0.09; 'subject:position': 0.09; 'python': 0.11; 'better:': 0.16; 'burak': 0.16; 'fine.': 0.16; 'fixing.': 0.16; 'formatting.': 0.16; 'from:addr:arskom.com.tr': 0.16; 'from:addr:burak.arslan': 0.16; 'from:name:burak arslan': 0.16; 'hack,': 0.16; 'message-id:@arskom.com.tr': 0.16; 'rationale': 0.16; 'received:arskomhosting.com': 0.16; 'smtp,': 0.16; 'subject:Add': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'app': 0.19; 'module': 0.19; 'trying': 0.19; "python's": 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'headers': 0.24; 'parse': 0.24; 'header': 0.24; 'mon,': 0.24; 'server.': 0.24; 'this:': 0.26; 'required.': 0.27; 'header:In- Reply-To:1': 0.27; 'appear': 0.29; 'correct': 0.29; 'rest': 0.29; 'chris': 0.29; '[1]': 0.29; "i'm": 0.30; 'work.': 0.31; 'that.': 0.31; '>>>>': 0.31; 'header,': 0.31; 'trivial': 0.31; 'figure': 0.32; 'supposed': 0.32; 'another': 0.32; 'says': 0.33; "i'd": 0.34; 'knowledge': 0.35; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'consistent': 0.36; 'smtp': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'should': 0.36; 'application': 0.37; 'being': 0.38; 'ends': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'easy': 0.60; "you're": 0.61; 'more': 0.64; 'received:': 0.65; 'understand,': 0.84; 'edwards': 0.91; 'reasons,': 0.91; 'anywhere,': 0.93 Date: Wed, 07 May 2014 18:06:31 +0300 From: Burak Arslan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Add "Received:" header to email msg in correct position? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399475193 news.xs4all.nl 2901 [2001:888:2000:d::a6]:57508 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71027 On 05/06/14 18:26, Grant Edwards wrote: > On 2014-05-06, Burak Arslan wrote: >> On 05/06/14 12:47, Chris Angelico wrote: >>> On Tue, May 6, 2014 at 7:15 PM, alister >>> 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