Sample code for 30+ languages & platforms
Swift

Add File Attachments to an Email

Demonstrates how to add one or more file attachments to an email.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let email = CkoEmail()!

    email.subject = "This is a test"
    email.body = "This is a test"
    email.from = "support@chilkatsoft.com"
    success = email.add(to: "Chilkat Admin", emailAddress: "admin@chilkatsoft.com")

    // To add file attachments to an email, call AddFileAttachment
    // once for each file to be attached.  The method returns
    // the content-type of the attachment if successful, otherwise
    // returns cknull
    var contentType: String?

    contentType = email.addFileAttachment(path: "something.pdf")
    if email.lastMethodSuccess != true {
        print("\(email.lastErrorText!)")
        return
    }

    contentType = email.addFileAttachment(path: "something.xml")
    if email.lastMethodSuccess != true {
        print("\(email.lastErrorText!)")
        return
    }

    contentType = email.addFileAttachment(path: "something.zip")
    if email.lastMethodSuccess != true {
        print("\(email.lastErrorText!)")
        return
    }

    success = email.saveEml(path: "email.eml")
    if success == false {
        print("\(email.lastErrorText!)")
        return
    }

    print("Saved EML!")

}