Sample code for 30+ languages & platforms
PureBasic

Prepend Added Header Fields to the Top

See more Email Object Examples

Demonstrates the Chilkat Email.PrependHeaders property. When true, header fields added through AddHeaderField or AddHeaderField2 are placed at the top of the header rather than appended at the bottom (the default is false). This affects only fields added after the property is set — it does not reorder fields that are already present. This example enables prepending and adds two custom header fields.

Background: In a MIME message the order of header fields is mostly cosmetic, but there are cases where "top vs bottom" matters — for instance, trace headers like Received are conventionally added to the top so the most recent hop appears first, and some pipelines expect certain fields early in the header block. PrependHeaders gives you control over placement when you add your own custom fields.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    ;  Demonstrates the Email.PrependHeaders property.  When true, header fields added via
    ;  AddHeaderField (or AddHeaderField2) are prepended to the top of the header instead of
    ;  appended to the bottom.  The default is false.

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Prepend headers demo")
    CkEmail::setCkFrom(email, "mary@example.com")

    ;  Cause subsequently-added header fields to go to the top of the header.
    CkEmail::setCkPrependHeaders(email, 1)

    CkEmail::ckAddHeaderField(email,"X-Custom-One","first value")
    CkEmail::ckAddHeaderField(email,"X-Custom-Two","second value")

    Debug CkEmail::ckHeader(email)


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure