Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.0.0+

Reply to an email and preserve its thread

See more Email Object Examples

Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set original [new_CkEmail]

set success [CkEmail_LoadEml $original "with-attachments.eml"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $original]
    delete_CkEmail $original
    exit
}

set reply [new_CkEmail]

set success [CkEmail_ToReply $original $reply]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $original]
    delete_CkEmail $original
    delete_CkEmail $reply
    exit
}

CkEmail_put_From $reply "Alex Rivera <alex@example.net>"
#  This is a reply to the author, not a reply-all. Set the destination explicitly.
CkEmail_ClearTo $reply
CkEmail_ClearCC $reply
CkEmail_ClearBcc $reply
set destination [CkEmail_getHeaderField $original "Reply-To"]
if {$destination == ""} then {
    set destination [CkEmail_from $original]
}

set success [CkEmail_AddMultipleTo $reply $destination]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $reply]
    delete_CkEmail $original
    delete_CkEmail $reply
    exit
}

#  Link this answer to its parent and preserve the earlier conversation references.
set parentId [CkEmail_getHeaderField $original "Message-ID"]
if {$parentId != ""} then {
    CkEmail_AddHeaderField $reply "In-Reply-To" $parentId
    set refs [new_CkStringBuilder]

    CkStringBuilder_Append $refs [CkEmail_getHeaderField $original "References"]
    if {[CkStringBuilder_get_Length $refs] > 0} then {
        CkStringBuilder_Append $refs " "
    }

    CkStringBuilder_Append $refs $parentId
    CkEmail_AddHeaderField $reply "References" [CkStringBuilder_getAsString $refs]
}

#  A short text reply has no use for the original inline artwork or nested correspondence.
CkEmail_RemoveAttachedMessages $reply
CkEmail_RemoveHtmlAlternative $reply
CkEmail_DropRelatedItems $reply
CkEmail_SetTextBody $reply "Thank you for your message." "text/plain"
if {[CkEmail_get_NumTo $reply] < 1} then {
    puts "Reply has no destination. Review the original sender headers."
    delete_CkEmail $original
    delete_CkEmail $reply
    delete_CkStringBuilder $refs
    exit
}

puts "Reply goes to: [CkEmail_getToAddr $reply 0]"
puts "In-Reply-To: [CkEmail_getHeaderField $reply In-Reply-To]"

set success [CkEmail_SaveEml $reply "reply.eml"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $reply]
    delete_CkEmail $original
    delete_CkEmail $reply
    delete_CkStringBuilder $refs
    exit
}

puts "Saved reply.eml."

delete_CkEmail $original
delete_CkEmail $reply
delete_CkStringBuilder $refs