Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70933 > unrolled thread

Add "Received:" header to email msg in correct position?

Started byGrant Edwards <invalid@invalid.invalid>
First post2014-05-05 19:51 +0000
Last post2014-05-07 14:33 +0000
Articles 20 on this page of 36 — 12 participants

Back to article view | Back to comp.lang.python


Contents

  Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-05 19:51 +0000
    Re: Add "Received:" header to email msg in correct position? Ethan Furman <ethan@stoneleaf.us> - 2014-05-05 13:01 -0700
      Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-05 20:58 +0000
        Re: Add "Received:" header to email msg in correct position? Tim Chase <python.list@tim.thechases.com> - 2014-05-05 16:09 -0500
          Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-06 02:13 +0000
            Re: Add "Received:" header to email msg in correct position? Steven D'Aprano <steve@pearwood.info> - 2014-05-06 08:22 +0000
              Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-06 14:12 +0000
        Re: Add "Received:" header to email msg in correct position? Terry Reedy <tjreedy@udel.edu> - 2014-05-06 14:16 -0400
        Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-07 10:13 +1000
    Re: Add "Received:" header to email msg in correct position? alister <alister.nospam.ware@ntlworld.com> - 2014-05-06 09:15 +0000
      Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-06 19:47 +1000
        Re: Add "Received:" header to email msg in correct position? alister <alister.nospam.ware@ntlworld.com> - 2014-05-06 10:26 +0000
          Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-06 20:41 +1000
      Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-06 14:15 +0000
        Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-06 14:19 +0000
        Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-07 00:26 +1000
        Re: Add "Received:" header to email msg in correct position? alister <alister.nospam.ware@ntlworld.com> - 2014-05-06 15:16 +0000
      Re: Add "Received:" header to email msg in correct position? Burak Arslan <burak.arslan@arskom.com.tr> - 2014-05-06 18:11 +0300
        Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-06 15:26 +0000
          Re: Add "Received:" header to email msg in correct position? Burak Arslan <burak.arslan@arskom.com.tr> - 2014-05-07 18:06 +0300
          Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-08 01:11 +1000
          Re: Add "Received:" header to email msg in correct position? Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-07 16:29 -0600
            Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-08 13:28 +0000
              Re: Add "Received:" header to email msg in correct position? Chris Angelico <rosuav@gmail.com> - 2014-05-08 23:47 +1000
              Re: Add "Received:" header to email msg in correct position? Ethan Furman <ethan@stoneleaf.us> - 2014-05-08 06:46 -0700
              Re: Add "Received:" header to email msg in correct position? Skip Montanaro <skip@pobox.com> - 2014-05-08 09:50 -0500
                Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-08 18:39 +0000
                  Re: Add "Received:" header to email msg in correct position? Tim Chase <python.list@tim.thechases.com> - 2014-05-08 13:48 -0500
                    Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-09 14:28 +0000
                  Re: Add "Received:" header to email msg in correct position? Ethan Furman <ethan@stoneleaf.us> - 2014-05-08 17:00 -0700
    Re: Add "Received:" header to email msg in correct position? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-05-07 08:58 +0200
      Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-07 14:32 +0000
        Re: Add "Received:" header to email msg in correct position? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-05-08 09:44 +0200
    Re: Add "Received:" header to email msg in correct position? Emre Hasegeli <emre@hasegeli.com> - 2014-05-07 11:11 +0300
    Re: Add "Received:" header to email msg in correct position? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-05-07 10:57 +0200
      Re: Add "Received:" header to email msg in correct position? Grant Edwards <invalid@invalid.invalid> - 2014-05-07 14:33 +0000

Page 1 of 2  [1] 2  Next page →


#70933 — Add "Received:" header to email msg in correct position?

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-05 19:51 +0000
SubjectAdd "Received:" header to email msg in correct position?
Message-ID<lk8q3j$3mg$2@reader1.panix.com>
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.

-- 
Grant Edwards               grant.b.edwards        Yow! With YOU, I can be
                                  at               MYSELF ...  We don't NEED
                              gmail.com            Dan Rather ...

[toc] | [next] | [standalone]


#70934

FromEthan Furman <ethan@stoneleaf.us>
Date2014-05-05 13:01 -0700
Message-ID<mailman.9687.1399323113.18130.python-list@python.org>
In reply to#70933
On 05/05/2014 12:51 PM, 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.

I don't know that it matters, but which Python version?

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#70935

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-05 20:58 +0000
Message-ID<lk8u1h$sid$1@reader1.panix.com>
In reply to#70934
On 2014-05-05, Ethan Furman <ethan@stoneleaf.us> 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

-- 
Grant Edwards               grant.b.edwards        Yow! over in west
                                  at               Philadelphia a puppy is
                              gmail.com            vomiting ...

[toc] | [prev] | [next] | [standalone]


#70936

FromTim Chase <python.list@tim.thechases.com>
Date2014-05-05 16:09 -0500
Message-ID<mailman.9688.1399324196.18130.python-list@python.org>
In reply to#70935

[Multipart message — attachments visible in raw view] — view raw

On 2014-05-05 20:58, Grant Edwards wrote:
> On 2014-05-05, Ethan Furman <ethan@stoneleaf.us> 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





[toc] | [prev] | [next] | [standalone]


#70940

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-06 02:13 +0000
Message-ID<lk9gfl$9eo$1@reader1.panix.com>
In reply to#70936
On 2014-05-05, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2014-05-05 20:58, Grant Edwards wrote:
>> On 2014-05-05, Ethan Furman <ethan@stoneleaf.us> 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.

Thanks.

There's is a somewhat messy way to do it by calling msg.items() to
retrieve all the current headers, removing all of them, and then
adding them all back (includeing the new one) in the order I want
them.

> 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).

[ insert_header() method example that lets you specify an index ]

A couple other options I was thinking about:

 * Just override __set_item__ so it treats 'Received' as a special
   case and inserts it before any existing 'Received' headers -- or
   prepends it if there aren't any.

 * Add a prepend_header() method that just sticks it at the top -- in
   most cases, I think that will be correct enough.

I think I like your suggestion better.  Since the keys() method
preserves the order of header keys, that makes figuring out the
insertion point trivial.

-- 
Grant

[toc] | [prev] | [next] | [standalone]


#70946

FromSteven D'Aprano <steve@pearwood.info>
Date2014-05-06 08:22 +0000
Message-ID<53689bb2$0$11109$c3e8da3@news.astraweb.com>
In reply to#70940
On Tue, 06 May 2014 02:13:09 +0000, Grant Edwards wrote:

> There's is a somewhat messy way to do it by calling msg.items() to
> retrieve all the current headers, removing all of them, and then adding
> them all back (includeing the new one) in the order I want them.

Does msg.items() return the actual message header list, or a copy? 
Because if it returns the list itself, you can just insert directly into 
the list using the insert method.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#70960

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-06 14:12 +0000
Message-ID<lkaql5$8pf$1@reader1.panix.com>
In reply to#70946
On 2014-05-06, Steven D'Aprano <steve@pearwood.info> wrote:
> On Tue, 06 May 2014 02:13:09 +0000, Grant Edwards wrote:
>
>> There's is a somewhat messy way to do it by calling msg.items() to
>> retrieve all the current headers, removing all of them, and then adding
>> them all back (includeing the new one) in the order I want them.
>
> Does msg.items() return the actual message header list, or a copy?

A shallow copy:

    def items(self):
        """Get all the message's header fields and values.

        These will be sorted in the order they appeared in the original
        message, or were added to the message, and may contain duplicates.
        Any fields deleted and re-inserted are always appended to the header
        list.
        """
        return self._headers[:]

> Because if it returns the list itself, you can just insert directly into 
> the list using the insert method.

-- 
Grant Edwards               grant.b.edwards        Yow! What I need is a
                                  at               MATURE RELATIONSHIP with a
                              gmail.com            FLOPPY DISK ...

[toc] | [prev] | [next] | [standalone]


#70981

FromTerry Reedy <tjreedy@udel.edu>
Date2014-05-06 14:16 -0400
Message-ID<mailman.9707.1399407827.18130.python-list@python.org>
In reply to#70935
On 5/5/2014 4:58 PM, Grant Edwards wrote:
> On 2014-05-05, Ethan Furman <ethan@stoneleaf.us> wrote:

>> I don't know that it matters, but which Python version?

I usually does these days.

> Sorry, should have mentioned it: 2.7.5

email has been improved with successive 3.x versions.
https://docs.python.org/3/library/email.html#package-history

There might be an email5 backport for 2.7 on pypi.

If the prepend requirement is covered by

"The email package is a library for managing email messages, including 
MIME and other RFC 2822-based message documents. It is specifically not 
designed to do any sending of email messages to SMTP (RFC 2821), NNTP, 
or other servers; those are functions of modules such as smtplib and 
nntplib. The email package attempts to be as RFC-compliant as possible, 
supporting in addition to RFC 2822, such MIME-related RFCs as RFC 2045, 
RFC 2046, RFC 2047, and RFC 2231."

and the current 3.4/5 version does not prepend and there is no existing 
tracker issue, then a new issue would seem to be appropriate.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#70997

FromChris Angelico <rosuav@gmail.com>
Date2014-05-07 10:13 +1000
Message-ID<mailman.9720.1399421641.18130.python-list@python.org>
In reply to#70935
On Wed, May 7, 2014 at 4:16 AM, Terry Reedy <tjreedy@udel.edu> wrote:
> If the prepend requirement is covered by
>
> "The email package is a library for managing email messages, including MIME
> and other RFC 2822-based message documents. It is specifically not designed
> to do any sending of email messages to SMTP (RFC 2821), NNTP, or other
> servers; those are functions of modules such as smtplib and nntplib. The
> email package attempts to be as RFC-compliant as possible, supporting in
> addition to RFC 2822, such MIME-related RFCs as RFC 2045, RFC 2046, RFC
> 2047, and RFC 2231."
>
> and the current 3.4/5 version does not prepend and there is no existing
> tracker issue, then a new issue would seem to be appropriate.

That's a bit tricky. RFC 2822 section 3.6.7 says the Received: headers
are "strictly informational, and any formal interpretation of them is
outside of the scope of this document", but it does reference RFC
2821. Should the addition of another Received header be part of
building an RFC 2822 compliant message, or should you build up a
message without that, and have one added at transport time?

I would say that, even if this isn't considered a bug (as in, failure
to comply with standards it claims to comply with), it would still be
a viable feature addition.

ChrisA

[toc] | [prev] | [next] | [standalone]


#70949

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-05-06 09:15 +0000
Message-ID<tK1av.250678$q95.71799@fx22.am4>
In reply to#70933
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.




-- 
BTW: I have a better name for the software .... Microsoft Internet
Exploder.
	-- George Bonser <grep@cris.com>

[toc] | [prev] | [next] | [standalone]


#70950

FromChris Angelico <rosuav@gmail.com>
Date2014-05-06 19:47 +1000
Message-ID<mailman.9693.1399369682.18130.python-list@python.org>
In reply to#70949
On Tue, May 6, 2014 at 7:15 PM, alister
<alister.nospam.ware@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".

ChrisA

[1] http://www.ietf.org/rfc/rfc2821.txt

[toc] | [prev] | [next] | [standalone]


#70952

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-05-06 10:26 +0000
Message-ID<5N2av.257203$q95.207421@fx22.am4>
In reply to#70950
On Tue, 06 May 2014 19:47:54 +1000, Chris Angelico wrote:

> On Tue, May 6, 2014 at 7:15 PM, alister
> <alister.nospam.ware@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".
> 
> ChrisA
> 
> [1] http://www.ietf.org/rfc/rfc2821.txt

oh well, so much for the easy route :-)
This suggests the email module could do with amending so that headers can 
be pretended as appended.




-- 
The strong give up and move away, while the weak give up and stay.

[toc] | [prev] | [next] | [standalone]


#70954

FromChris Angelico <rosuav@gmail.com>
Date2014-05-06 20:41 +1000
Message-ID<mailman.9695.1399372909.18130.python-list@python.org>
In reply to#70952
On Tue, May 6, 2014 at 8:26 PM, alister
<alister.nospam.ware@ntlworld.com> wrote:
> On Tue, 06 May 2014 19:47:54 +1000, Chris Angelico wrote:
>
>> On Tue, May 6, 2014 at 7:15 PM, alister
>> <alister.nospam.ware@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".
>>
>> ChrisA
>>
>> [1] http://www.ietf.org/rfc/rfc2821.txt
>
> oh well, so much for the easy route :-)
> This suggests the email module could do with amending so that headers can
> be pretended as appended.

Even if it's special-cased as a dedicated "prepend received header"
method, that would probably do. I can't think of any other headers
where you need to specifically order them rather than appending.

ChrisA

[toc] | [prev] | [next] | [standalone]


#70961

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-06 14:15 +0000
Message-ID<lkaqpc$8pf$2@reader1.panix.com>
In reply to#70949
On 2014-05-06, alister <alister.nospam.ware@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?

I couldn't find it in an RFC.  But every reference I could find that
mentioned Received: headers did say that you read them from the bottom
up (most recent is on top).

> 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.

I'd like to do it the right way whether it's required by the letter of
the law or not.

-- 
Grant Edwards               grant.b.edwards        Yow! I'm GLAD I
                                  at               remembered to XEROX all
                              gmail.com            my UNDERSHIRTS!!

[toc] | [prev] | [next] | [standalone]


#70962

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-06 14:19 +0000
Message-ID<lkar1g$8pf$3@reader1.panix.com>
In reply to#70961
On 2014-05-06, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2014-05-06, alister <alister.nospam.ware@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?
>
> I couldn't find it in an RFC.

I was looking in the message format RFCs, and as has already been
pointed out, it's in the SMTP spec (RFC2821).  Since there _is_ an RFC
that specifically states you MUST prepend a header line to a message,
this seems to be a hole in the email module...

-- 
Grant Edwards               grant.b.edwards        Yow! If I am elected no one
                                  at               will ever have to do their
                              gmail.com            laundry again!

[toc] | [prev] | [next] | [standalone]


#70963

FromChris Angelico <rosuav@gmail.com>
Date2014-05-07 00:26 +1000
Message-ID<mailman.9699.1399386394.18130.python-list@python.org>
In reply to#70961
On Wed, May 7, 2014 at 12:15 AM, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2014-05-06, alister <alister.nospam.ware@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?
>
> I couldn't find it in an RFC.  But every reference I could find that
> mentioned Received: headers did say that you read them from the bottom
> up (most recent is on top).

Yeah, I went looking for it in 2822 but didn't find much. After some
digging, found it in 2821, section 3.8.2. It's not dwelled on, but the
word "prepend" is used, which to my mind is a clear indication of
necessary ordering.

ChrisA

[toc] | [prev] | [next] | [standalone]


#70969

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-05-06 15:16 +0000
Message-ID<a17av.328995$5V5.89218@fx08.am4>
In reply to#70961
On Tue, 06 May 2014 14:15:08 +0000, Grant Edwards wrote:
> 
> I'd like to do it the right way whether it's required by the letter of
> the law or not.
So do I , although i might have left a cosmetic issue until the rest of 
the app was functioning as desired.

As wiser minds than mine have now confirmed order is important this point 
is of course moot - sorry.




-- 
Guns don't kill people.  It's those damn bullets.  Guns just make them go
really really fast.
        -- Jake Johanson

[toc] | [prev] | [next] | [standalone]


#70967

FromBurak Arslan <burak.arslan@arskom.com.tr>
Date2014-05-06 18:11 +0300
Message-ID<mailman.9701.1399389121.18130.python-list@python.org>
In reply to#70949
On 05/06/14 12:47, Chris Angelico wrote:
> On Tue, May 6, 2014 at 7:15 PM, alister
> <alister.nospam.ware@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.

Best,
Burak

[toc] | [prev] | [next] | [standalone]


#70971

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-06 15:26 +0000
Message-ID<lkauuj$lpl$1@reader1.panix.com>
In reply to#70967
On 2014-05-06, Burak Arslan <burak.arslan@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.ware@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.

-- 
Grant Edwards               grant.b.edwards        Yow! ... I want a COLOR
                                  at               T.V. and a VIBRATING BED!!!
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#71027

FromBurak Arslan <burak.arslan@arskom.com.tr>
Date2014-05-07 18:06 +0300
Message-ID<mailman.9736.1399475193.18130.python-list@python.org>
In reply to#70971
On 05/06/14 18:26, Grant Edwards wrote:
> On 2014-05-06, Burak Arslan <burak.arslan@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.ware@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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web