Email module, how to add header to the top of an email?

Discussion in 'Python' started by David Erickson, Jan 24, 2008.

  1. I have been using the Email module and Message class for awhile,
    however I have been unable to find a way to add a header to the top of
    the email similar to what is done with Received: headers... the
    add_header method only appends to the bottom. Is there someway this
    can be done?

    Thanks
    David
    David Erickson, Jan 24, 2008
    #1
    1. Advertising

  2. On Thursday 24 January 2008 20:32 David Erickson wrote:

    > I have been using the Email module and Message class for awhile,
    > however I have been unable to find a way to add a header to the top of
    > the email similar to what is done with Received: headers... the
    > add_header method only appends to the bottom.  Is there someway this
    > can be done?



    if by bottom you mean added as the "new last" header than you don't have to
    care, afaik email headers do not have a notion of order

    e.g

    To:
    From:

    is equal to

    From:
    To:


    if by bottom you mean it's appended to the body...well that is a problem :)

    hth
    martin

    --
    http://noneisyours.marcher.name
    http://feeds.feedburner.com/NoneIsYours

    You are not free to read this message,
    by doing so, you have violated my licence
    and are required to urinate publicly. Thank you.
    Martin Marcher, Jan 24, 2008
    #2
    1. Advertising

  3. On Jan 24, 2:41 pm, Martin Marcher <> wrote:
    > On Thursday 24 January 2008 20:32 David Erickson wrote:
    >
    > > I have been using the Email module and Message class for awhile,
    > > however I have been unable to find a way to add a header to the top of
    > > the email similar to what is done with Received: headers... the
    > > add_header method only appends to the bottom. Is there someway this
    > > can be done?

    >
    > if by bottom you mean added as the "new last" header than you don't have to
    > care, afaik email headers do not have a notion of order
    >
    > e.g
    >
    > To:
    > From:
    >
    > is equal to
    >
    > From:
    > To:
    >
    > if by bottom you mean it's appended to the body...well that is a problem :)
    >
    > hth
    > martin


    Bottom of the headers... but I am looking to insert at the top, and re-
    ordering/inserting does matter depending on what type of header you
    are, received headers for example must be inserted at the top of the
    header list so you can watch the progression of the email. I was
    unable to find a clean way to do this via the API which seems very
    strange to me.. but perhaps I am just missing something?

    Thanks,
    -David
    David Erickson, Jan 25, 2008
    #3
  4. Am 25.01.2008, 06:21 Uhr, schrieb David Erickson <>:

    > Bottom of the headers... but I am looking to insert at the top, and re-
    > ordering/inserting does matter depending on what type of header you
    > are, received headers for example must be inserted at the top of the
    > header list so you can watch the progression of the email. I was
    > unable to find a clean way to do this via the API which seems very
    > strange to me.. but perhaps I am just missing something?


    I think your are really missing something. First, Email-headers are
    unordered
    and every relay can, and probably will, rearrange them, add or delete
    headers.
    You therefore most definitely should not add any headers which are
    position-dependent.
    If you want to track mails somehow, add headers with timestamps.
    This way you can watch the progression of an email without being
    dependend on "sorted" headerlines.




    --
    Greetz, lunqual - http://www.lunqual.de
    http://www.42pixels.de - Bilder
    http://www.rezeptbuch-pro.de - Software für den Destillateur
    Karlheinz Klingbeil, Jan 25, 2008
    #4
  5. On Jan 25, 5:04 am, "Karlheinz Klingbeil"
    <> wrote:
    > Am 25.01.2008, 06:21 Uhr, schrieb David Erickson <>:
    >
    > > Bottom of the headers... but I am looking to insert at the top, and re-
    > > ordering/inserting does matter depending on what type of header you
    > > are, received headers for example must be inserted at the top of the
    > > header list so you can watch the progression of the email. I was
    > > unable to find a clean way to do this via the API which seems very
    > > strange to me.. but perhaps I am just missing something?

    >
    > I think your are really missing something. First, Email-headers are
    > unordered
    > and every relay can, and probably will, rearrange them, add or delete
    > headers.
    > You therefore most definitely should not add any headers which are
    > position-dependent.
    > If you want to track mails somehow, add headers with timestamps.
    > This way you can watch the progression of an email without being
    > dependend on "sorted" headerlines.


    This is incorrect, quoting directly from RFC 2822:

    It is important to note that the header fields are not guaranteed
    to
    be in a particular order. They may appear in any order, and they
    have been known to be reordered occasionally when transported over
    the Internet. However, for the purposes of this standard, header
    fields SHOULD NOT be reordered when a message is transported or
    transformed. More importantly, the trace header fields and resent
    header fields MUST NOT be reordered, and SHOULD be kept in blocks
    prepended to the message. See sections 3.6.6 and 3.6.7 for more
    information.

    Trace header fields are not to be ordered, and should be prepended
    when added to a message. Now that said I am not trying to track
    anything, I simply want to prepend my additional headers onto the top
    of the email, and seem to be unable to do that via the API, which I
    think ought to be possible. If for example I was writing some kind of
    an SMTP server with Python and needed to add said trace headers they
    would need to be prepended, and currently cannot be (without doing it
    by hand).

    -David
    David Erickson, Jan 26, 2008
    #5
  6. Am 26.01.2008, 01:46 Uhr, schrieb David Erickson <>:

    > It is important to note that the header fields are not guaranteed
    > to
    > be in a particular order. They may appear in any order, and they
    > have been known to be reordered occasionally when transported over
    > the Internet. However, for the purposes of this standard, header
    > fields SHOULD NOT be reordered when a message is transported or
    > transformed. More importantly, the trace header fields and resent
    > header fields MUST NOT be reordered, and SHOULD be kept in blocks
    > prepended to the message. See sections 3.6.6 and 3.6.7 for more
    > information.
    >
    > Trace header fields are not to be ordered, and should be prepended
    > when added to a message. Now that said I am not trying to track
    > anything, I simply want to prepend my additional headers onto the top
    > of the email, and seem to be unable to do that via the API, which I
    > think ought to be possible. If for example I was writing some kind of
    > an SMTP server with Python and needed to add said trace headers they
    > would need to be prepended, and currently cannot be (without doing it
    > by hand).
    >
    > -David

    You're right on that point, though it says clearly "It is important to
    note that the header fields are not guaranteed to be in a particular
    order."

    So it's in my opinion a good idea not to expect headers to be ordered and
    add headers in a way which doesn't conflict with a *possible* reordering.


    --
    Greetz, lunqual - http://www.lunqual.de
    http://www.42pixels.de - Bilder
    http://www.rezeptbuch-pro.de - Software für den Destillateur
    Karlheinz Klingbeil, Jan 26, 2008
    #6
  7. On Sat, 26 Jan 2008 11:10:14 +0100, "Karlheinz Klingbeil"
    <> declaimed the following in
    comp.lang.python:

    > You're right on that point, though it says clearly "It is important to
    > note that the header fields are not guaranteed to be in a particular
    > order."
    >
    > So it's in my opinion a good idea not to expect headers to be ordered and
    > add headers in a way which doesn't conflict with a *possible* reordering.


    And, from what I've seen, the only headers that do show order are
    those added in "passing" mail -- something easily done during the
    streaming of the message... Add a "received" header, then copy the rest
    of the message as-is, with no interpretation of the contents.

    I have the impression most of the email libraries for Python are
    aimed at the user clients, not transfer utilities.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 27, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederic HOUDE
    Replies:
    4
    Views:
    586
    Frederic HOUDE
    Aug 31, 2004
  2. Replies:
    1
    Views:
    308
    Diez B. Roggisch
    Oct 29, 2006
  3. Cuthbert
    Replies:
    8
    Views:
    418
    Ancient_Hacker
    Sep 13, 2006
  4. mlt
    Replies:
    2
    Views:
    808
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Vít Ondruch

    Subclassing in module from top module?

    Vít Ondruch, Oct 12, 2009, in forum: Ruby
    Replies:
    3
    Views:
    108
    Vít Ondruch
    Oct 12, 2009
Loading...

Share This Page