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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    let email = CkoEmail()!
    success = email.loadEml(mimePath: "with-attachments.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    print("From: \(email.from!)")
    print("Subject: \(email.subject!)")
    print("Date: \(email.emailDateStr!)")
    var i: Int = 0
    while (i < email.numTo.intValue) {
        print("To: \(email.get(toName: i)!)  \(email.get(toAddr: i)!)")
        i = i + 1
    }

    var referenceId: String? = email.getHeaderField(fieldName: "X-Reference-ID")
    if email.lastMethodSuccess == true {
        print("Reference: \(referenceId!)")
    }

    if email.hasPlainTextBody() == true {
        print("\(email.getPlainTextBody()!)")
    }
    else {
        if email.hasHtmlBody() == true {
            let reader = CkoHtmlToText()!
            print("\(reader.toText(html: email.getHtmlBody())!)")
        }
        else {
            print("No text or HTML body.")
        }

    }


}