Sample code for 30+ languages & platforms
Rust

Embed Image in HTML Email

Demonstrates how to create and send an HTML email with an embedded image.

Chilkat Rust Downloads

Rust

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

// The mailman object is used for sending and receiving email.
let mailman = chilkat::MailMan::new();

// Set the SMTP server.
mailman.set_smtp_host("smtp.comcast.net");

// Create a new email object
let email = chilkat::Email::new();

// Add an embedded image to the HTML email.
let file_on_disk = "images/dude2.gif".to_string();
let file_path_in_html = "dudeAbc.gif".to_string();

// Embed the GIF image in the email.
if email.add_related_file2(&file_on_disk, &file_path_in_html).is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

// The src attribute for the image tag is set to the filePathInHtml:
let html_body = "<html><body>Embedded Image:<br><img src=\"dudeAbc.gif\"></body></html>".to_string();

// Set the basic email stuff: HTML body, subject, "from", "to";
email.set_html_body(&html_body);
email.set_subject("Rust HTML email with an embedded image.");
let _ = email.add_to("Admin", "admin@chilkatsoft.com").is_ok();
email.set_from("Chilkat Support <support@chilkatsoft.com>");

if mailman.send_email(&email).is_err() {
    println!("{}", mailman.last_error_text());
} else {
    println!("Mail Sent!");
}