Sample code for 30+ languages & platforms
Tcl

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set emlPath "C:/AAWorkarea/beatrix/roesner.eml"

set mime [new_CkMime]

set success [CkMime_LoadMimeFile $mime $emlPath]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkMime $mime
    exit
}

puts "---- MIME structure ----"
puts [CkMime_getStructure $mime text]
puts "------------------------"

set email [new_CkEmail]

set success [CkEmail_LoadEml $email $emlPath]

# Was this a signed and/or encrypted email?
# If so, then loading the .eml automatically unwraps
# (i.e. verifies signatures and decrypts) and the resultant
# email is what existed prior to signing/encrypting.
puts "Email was Signed: [CkEmail_get_ReceivedSigned $email]"
puts "Email was Encrypted: [CkEmail_get_ReceivedEncrypted $email]"
if {[CkEmail_get_ReceivedSigned $email] == 1} then {
    puts "Signature(s) valid = [CkEmail_get_SignaturesValid $email]"
}

if {[CkEmail_get_ReceivedEncrypted $email] == 1} then {
    puts "Decrypted successfully = [CkEmail_get_Decrypted $email]"
}

set i 0
set numAttach [CkEmail_get_NumAttachments $email]
puts "Number of attachments = $numAttach"

while {$i < $numAttach} {
    puts "---- Attachment $i"

    # Examine the filename (if any)
    puts "filename: [CkEmail_getAttachmentFilename $email $i]"
    # Examine the content-ID (if any)
    puts "Content-ID: [CkEmail_getAttachmentContentID $email $i]"
    # Examine the content-type
    puts "Content-Type: [CkEmail_getAttachmentContentType $email $i]"
    # Examine the content-disposition
    puts "Content-Disposition[CkEmail_getAttachmentHeader $email $i content-disposition]"
    # Examine the attachment size:
    puts "Size (in bytes) of the attachment: [CkEmail_GetAttachmentSize $email $i]"

    set i [expr $i + 1]
}
puts "--"

# Now for the related items.

# Note: A MIME sub-part can potentially be both a related item AND an attachment.
# The typical case is when the item is contained under the multipart/related enclosure and 
# the item also has a "Content-Disposition" header indicating "attachment".
# The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
# Related items and attachments are not necessarily mutually exclusive.

set numRelated [CkEmail_get_NumRelatedItems $email]
puts "Number of related items = $numRelated"
set i 0
while {$i < $numRelated} {
    puts "---- Related Item $i"

    # Examine the filename (if any)
    puts "filename: [CkEmail_getRelatedFilename $email $i]"
    # Examine the content-ID (if any)
    puts "Content-ID: [CkEmail_getRelatedContentID $email $i]"
    # Examine the content-type
    puts "Content-Type: [CkEmail_getRelatedContentType $email $i]"
    # Examine the content-location (if any)
    puts "Content-Location[CkEmail_getRelatedContentLocation $email $i]"

    set i [expr $i + 1]
}

# The email could also have attached messages.
# An attached message is another email that was attached to this email.
set em [new_CkEmail]

set numAttachedMessages [CkEmail_get_NumAttachedMessages $email]
puts "Number of attached messages = $numAttachedMessages"
set i 0
while {$i < $numAttachedMessages} {
    puts "---- Attached message $i"

    # Examine the attached email
    CkEmail_GetAttachedEmail $email $i $em
    puts "from: [CkEmail_from $em]"
    puts "subject: [CkEmail_subject $em]"
    set i [expr $i + 1]
}

# An email could also be a multipart/report email. 
# This is a DSN (Delivery Status Notification)
# The NumReports property indicates how many "reports" exist.
set numReports [CkEmail_get_NumReports $email]
puts "Number of reports = $numReports"
set i 0
while {$i < $numReports} {
    puts "---- Report $i"
    # Get the raw report data...
    puts [CkEmail_getReport $email $i]
    set i [expr $i + 1]
}

# If the email is a multipart/report, then the information
# from the message/delivery-status part of the email can be retrieved:
if {[CkEmail_IsMultipartReport $email] == 1} then {

    puts "--- Delivery Status Information:"
    puts "Status: [CkEmail_getDeliveryStatusInfo $email Status]"
    puts "Action: [CkEmail_getDeliveryStatusInfo $email Action]"
    puts "Reporting-MTA: [CkEmail_getDeliveryStatusInfo $email Reporting-MTA]"

    set jsonDsnInfo [new_CkJsonObject]

    CkEmail_GetDsnInfo $email $jsonDsnInfo
    CkJsonObject_put_EmitCompact $jsonDsnInfo 0
    puts [CkJsonObject_emit $jsonDsnInfo]
}


delete_CkMime $mime
delete_CkEmail $email
delete_CkEmail $em
delete_CkJsonObject $jsonDsnInfo