Zig
Zig
Create HTML Email Reply
See more Email Object Examples
The goal of this example is to create a new HTML body where the new reply HTML is at the top, and the original HTML body is below.The .eml files for this example are available at HTML Reply Email Samples (.eml)
Note: This is only one of a million ways to do it..
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const email = try chilkat.Email.init();
defer email.deinit();
// To create this example, I used Mozilla Thunderbird (with a GMail accont)
// to first send a simple HTML email to my chilkatsoft.com email address. I then
// used Thunderbird to reply with more HTML.
// I then saved both emails as .eml files. These are available at
// http://chilkatdownload.com/example_data/htmlreplyemail.zip
// Load the .eml for the 1st email that was received. This is the original
// HTML email before the reply HTML is added.
email.loadEml("qa_data/eml/receivedHtmlEmail.eml") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
// If this email doesn't have an HTML body, then do something else...
// (that's for another example maybe?)
if (!email.hasHtmlBody()) {
std.debug.print("Email does not have an HTML body..\n", .{});
return;
}
// Get the HTML body in a StringBuilder..
const sb_html = try chilkat.StringBuilder.init();
defer sb_html.deinit();
sb_html.append(try email.getHtmlBody(alloc)) catch {};
// The HTML body contains this:
// <html><head>
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
// </head>
// <body bgcolor="#FFFFFF" text="#000000">
// <font face="Calibri"><br>
// This is a test <font color="#3366ff"><i>HTML email</i></font>...<br>
// <br>
// <br>
// </font>
// </body>
// </html>
// Make sure the BODY tags are lowercase.
var num_replaced: i32 = sb_html.replace("<BODY", "<body");
num_replaced = sb_html.replace("</BODY", "</body");
// Get the HTML within the "body" open/close tags
const sb_body_content = try chilkat.StringBuilder.init();
defer sb_body_content.deinit();
sb_body_content.append(try sb_html.getAfterBetween(alloc, "<body", ">", "</body>")) catch {};
// Replace what we just extracted with a marker..
num_replaced = sb_html.replace(try sb_body_content.getAsString(alloc), "TO_BE_UPDATED");
// Create a template for the new body:
const sb_template = try chilkat.StringBuilder.init();
defer sb_template.deinit();
const b_crlf = true;
sb_template.appendLine("REPLY_HTML_CONTENT", b_crlf) catch {};
sb_template.appendLine("<br>", b_crlf) catch {};
sb_template.appendLine("<div class=\"moz-signature\">Best Regards,<br>", b_crlf) catch {};
sb_template.appendLine(" John Smith<br>", b_crlf) catch {};
sb_template.appendLine(" Chilkat Software, Inc.<br>", b_crlf) catch {};
sb_template.appendLine(" <p>", b_crlf) catch {};
sb_template.appendLine(" <a href=\"https://twitter.com/chilkatsoft\">Follow Chilkat on", b_crlf) catch {};
sb_template.appendLine(" Twitter</a>", b_crlf) catch {};
sb_template.appendLine(" </p>", b_crlf) catch {};
sb_template.appendLine("</div>", b_crlf) catch {};
sb_template.appendLine("<div class=\"moz-cite-prefix\">On 2/8/2017 4:25 PM, Chilkat Support", b_crlf) catch {};
sb_template.appendLine(" wrote:<br>", b_crlf) catch {};
sb_template.appendLine("</div>", b_crlf) catch {};
sb_template.appendLine("<blockquote>", b_crlf) catch {};
sb_template.appendLine("ORIGINAL_BODY_CONTENT", b_crlf) catch {};
sb_template.appendLine("</blockquote>", b_crlf) catch {};
sb_template.appendLine("<br>", b_crlf) catch {};
// Replace the markers in the template with actual HTML.
// First our reply HTML:
const my_html_reply_body = "This is my <font color=\"#6600cc\"><b>reply HTML</b></font><br>";
num_replaced = sb_template.replace("REPLY_HTML_CONTENT", my_html_reply_body);
// Now for the original HTML body content:
num_replaced = sb_template.replace("ORIGINAL_BODY_CONTENT", try sb_body_content.getAsString(alloc));
// Insert into sbHtml:
num_replaced = sb_html.replace("TO_BE_UPDATED", try sb_template.getAsString(alloc));
// Use sbHtml as the new HTML body..
email.setHtmlBody(try sb_html.getAsString(alloc));
// Examine the result HTML.
std.debug.print("{s}\n", .{try sb_html.getAsString(alloc)});
// Save the email.
email.saveEml("qa_output/emailWithUpdatedHtmlBody.eml") catch {};
// Note: To finish creating the reply email, modify the To/From/CC headers,
// the subject, etc. You may also with to iterate over the headers (from 1 to NumHeaderFields)
// and selectively remove all except the particular headers you wish to keep.
// You should keep: MIME-Version, Content-Type, Message-ID. Chilkat will automaticaly
// generate and replace the Message-ID at the time of sending. You'll want to replace
// the Date header with the current date/time.
//
}