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

Forward an email as an attachment

See more Email Object Examples

Attach the original Email to a new message as a message/rfc822 part. The recipient can open the attached email and read its original headers and body.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let original = CkoEmail()!
    success = original.loadEml(mimePath: "message.eml")
    if success != true {
        print("\(original.lastErrorText!)")
        return
    }

    let email = CkoEmail()!
    email.from = "Sam Lee <sam@example.com>"
    success = email.add(to: "Alex Rivera", emailAddress: "alex@example.net")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    email.subject = "Forwarded message"
    email.body = "The original email is attached."
    success = email.attach(email: original)
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    success = email.saveEml(path: "forward-as-attachment.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    print("Saved forward-as-attachment.eml.")

}