Rust
Rust
SendGrid HTML Email with Embedded Images
See more SendGrid Examples
Demonstrates how to send an HTML email with embedded images using SendGrid.Chilkat Rust Downloads
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code
let req = chilkat::HttpRequest::new();
let http = chilkat::Http::new();
// First.. load a JPG file and build an HTML img tag with the JPG data inline encoded.
// Our HTML img tag will look like this:
// <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA3ADcAA..." alt="" style="border-width:0px;" />
let bd_jpg = chilkat::BinData::new();
if bd_jpg.load_file("qa_data/jpg/starfish.jpg").is_err() {
println!("Failed to load JPG file.");
return;
}
// Note: HTML containing embedded base64 image data may not be visible in all email clients.
// For example, GMail might not display the image, but viewing in Outlook might be OK.
let sb_html = chilkat::StringBuilder::new();
let _ = sb_html.append("<html><body><b>This is a test<b><br /><img src=\"data:image/jpeg;base64,");
// Append the base64 image data to sbHtml.
let _ = bd_jpg.get_encoded_sb("base64", &sb_html);
let _ = sb_html.append("\" alt=\"\" style=\"border-width:0px;\" /></body></html>");
let json = chilkat::JsonObject::new();
let _ = json.update_string("personalizations[0].to[0].email", "matt@chilkat.io");
let _ = json.update_string("personalizations[0].to[0].name", "Matt");
let _ = json.update_string("from.email", "admin@chilkatsoft.com");
let _ = json.update_string("subject", "Test HTML email with images");
let _ = json.update_string("content[0].type", "text/html");
let _ = json.update_sb("content[0].value", &sb_html);
http.set_auth_token("SENDGRID_API_KEY");
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.sendgrid.com/v3/mail/send", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("response status code: {}", resp.status_code());
// Display the response.
// If successful, the response code is 202 and the response body string is empty.
// (The response body string may also be empty for error response codes.)
println!("{}", resp.body_str());