Swift Requires Chilkat v11.0.0+
Swift
Create an email with plain-text and HTML bodies
See more Email Object Examples
Set the From and Reply-To addresses, add a recipient, and provide plain-text and HTML versions of the same body. Save the message as text-and-html.eml.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let email = CkoEmail()!
email.fromName = "Sam Lee"
email.fromAddress = "sam@example.com"
email.replyTo = "replies@example.com"
success = email.add(to: "Alex Rivera", emailAddress: "alex@example.net")
if success != true {
print("\(email.lastErrorText!)")
return
}
email.subject = "Sample message"
email.charset = "utf-8"
// A custom header can carry a reference alongside the standard message fields.
email.addHeaderField(fieldName: "X-Reference-ID", fieldValue: "REF-42")
success = email.addPlainTextAlternativeBody(body: "Read more at https://example.com/details.")
if success != true {
print("\(email.lastErrorText!)")
return
}
success = email.addHtmlAlternativeBody(body: "<p><a href='https://example.com/details'>Read more</a></p>")
if success != true {
print("\(email.lastErrorText!)")
return
}
success = email.saveEml(path: "text-and-html.eml")
if success != true {
print("\(email.lastErrorText!)")
return
}
print("Saved text-and-html.eml.")
}