Swift
Swift
Send HTML Email with Image Downloaded from URL
Demonstrates how to compose an HTML email with an embedded image where the image data is downloaded from a URL.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First download the image we'll be adding to the HTML "img" tag.
let bdJpg = CkoBinData()!
let http = CkoHttp()!
success = http.quickGetBd(url: "https://www.chilkatsoft.com/images/starfish.jpg", binData: bdJpg)
if success == false {
print("\(http.lastErrorText!)")
return
}
let mailman = CkoMailMan()!
// Use your SMTP server..
mailman.smtpHost = "smtp.yourserver.com"
mailman.smtpPort = 587
mailman.startTLS = true
// Set the SMTP login/password
mailman.smtpUsername = "my_login"
mailman.smtpPassword = "my_password"
// Create an HTML email.
let email = CkoEmail()!
email.subject = "HTML Email with Image"
email.from = "Dave <somebody@mydomain.com>"
email.add(to: "Chilkat", emailAddress: "info@chilkatsoft.com")
var html: String? = "<html><body><p>This is an HTML email with an embedded image.</p><p><img src=\"starfish.jpg\" /></p></body></html>"
email.setHtmlBody(html: html)
// Note: The "starfish.jpg" here must match the name in the "img" tag's "src" attribute in the HTML above.
success = email.addRelatedBd2(binData: bdJpg, fileNameInHtml: "starfish.jpg")
if success == false {
print("\(email.lastErrorText!)")
return
}
email.saveEml(path: "qa_output/out.eml")
// success = mailman.SendEmail(email);
// if (success == ckfalse) {
// println mailman.LastErrorText;
// return;
// }
//
// ignore = mailman.CloseSmtpConnection();
print("Success.")
}