Sample code for 30+ languages & platforms
Swift

Convert HTML Web Page to Email and Send

See more MHT / HTML Email Examples

Converts an HTML page at a URL into an email with embedded images and sends it.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let mailman = CkoMailMan()!

    // The MHT component can be used to convert an HTML page
    // from a URL, file, or in-memory HTML into an email
    // with embedded images and style sheets.
    let mht = CkoMht()!

    let email = CkoEmail()!

    // There are two ways of embedding images in emails: with CIDs
    // and without.  When using CIDs, the IMG SRC attributes use
    // urls that begin with "cid:" and the corresponding image
    // embedded within the email includes a Content-ID header field.
    // When not using CIDs, the IMG SRC attribute can have a URL,
    // path, etc. and the corresponding image embedded within
    // the email has a matching Content-Location header field.

    // When testing with GMail, if CIDs are used the email will
    // be displayed with images blocked (by default) unless
    // the user allows them to be displayed.  Without CIDs,
    // the images are automatically displayed.

    // When testing with Eudora, Mozilla Thunderbird, and Outlook Express, 
    // embedded images display correctly with our without CIDs.

    // When testing with Yahoo! Mail, embedded images display OK.
    // However, the Yahoo! Mail user has the option of blocking
    // images.  If this is turned on, the CID images will still 
    // display correctly even though images are blocked.  Why?
    // It's because when viewing the email there is no external
    // HTTP request to fetch an image.  Therefore, a spammer 
    // cannot get an indication that you've read the email. 
    // However, if CIDs are not used, the images are blocked by 
    // any Yahoo! Mail user that has image blocking turned on --
    // even though the images are embedded.

    // I haven't tested Hotmail yet...
    // Regardless, there is no perfect solution.  If CIDs are used,
    // GMail users may block your embedded images, if CIDs are not
    // used, Yahoo! Mail users may block your embedded images.

    // I recommend setting using CIDs.  This is what Mozilla Thunderbird
    // does by default, and it's the more clear an unambiguous way
    // to indicate that images are indeed embedded.
    mht.useCids = true

    var emlStr: String? = mht.getEML(url: "http://www.bonairefishing.com/")
    if mht.lastMethodSuccess != true {
        print("\(mht.lastErrorText!)")
        return
    }

    success = email.set(fromMimeText: emlStr)
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    email.subject = "Test HTML email"

    // This example is tested in:
    // Outlook 2000, Eudora 7, Mozilla Thunderbird 1.5.0.9, Outlook Express 6, GMail, Yahoo Mail
    success = email.add(to: "Chilkat Support", emailAddress: "support@chilkatsoft.com")

    // Note: Chilkat does not regularly check our GMail and Yahoo
    // email accounts.  Please send support email to support@chilkatsoft.com
    success = email.add(to: "Chilkat on Yahoo", emailAddress: "chilkat_software@yahoo.com")
    success = email.add(to: "Chilkat on GMail", emailAddress: "chilkat.support@gmail.com")

    email.from = "chilkat@live.com"

    // Send email using smtp.live.com
    mailman.smtpHost = "smtp.live.com"
    mailman.smtpUsername = "chilkat@live.com"
    mailman.smtpPassword = "myPassword"
    mailman.smtpPort = 587
    mailman.startTLS = true

    success = mailman.sendEmail(email: email)
    if success != true {
        print("\(mailman.lastErrorText!)")
        return
    }

    success = mailman.closeSmtpConnection()
    if success != true {
        print("\(mailman.lastErrorText!)")
        return
    }

    print("HTML Email Sent!")

}