Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Create DSN (Delivery Status Notification) Email

See more Email Object Examples

Demonstrates how to create a DSN (Delivery Status Notification) Email having the format as defined in RFC 3464.

Chilkat Zig Downloads

Zig
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();

    email.setSubject("Test");
    email.setFrom("joe@example.com");
    email.setBody("This is a test");
    email.addTo("", "recipient@example.com") catch {};

    std.debug.print("{s}\n", .{try email.getMime(alloc)});
    std.debug.print("\n", .{});
    std.debug.print("-------------------------------------------------------------\n", .{});
    std.debug.print("\n", .{});

    // This is the email we just created:

    // MIME-Version: 1.0
    // Date: Mon, 12 May 2025 11:13:15 -0500
    // Message-ID: <917FA49F75544EF51948B0A52F403B925B51073F@SLICE>
    // Content-Type: text/plain; charset=us-ascii; format=flowed
    // Content-Transfer-Encoding: 7bit
    // X-Priority: 3 (Normal)
    // Subject: Test
    // From: joe@example.com
    // To: recipient@example.com
    //
    // This is a test

    // -----------------------------------------------------------------
    // Convert the above email into a DSN (Delivery Status Notification)

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("DeliveryStatusFields");
    xml.newChild2("Final-Recipient", "rfc822; recipient@example.com");
    xml.newChild2("Action", "failed");
    xml.newChild2("Status", "5.1.2");
    xml.newChild2("Diagnostic-Code", "smtp; 550 5.1.2 Host unknown (Domain name not found)");
    const dt_now = try chilkat.DateTime.init();
    defer dt_now.deinit();
    dt_now.setFromCurrentSystemTime() catch {};
    xml.newChild2("Last-Attempt-Date", try dt_now.getAsRfc822(alloc, true));

    const header_only = true;
    const sb_text = try chilkat.StringBuilder.init();
    defer sb_text.deinit();
    sb_text.append("This is an automatically generated Delivery Status Notification.\r\n\r\n") catch {};
    sb_text.append("Delivery to the following recipient failed permanently:\r\n\r\n") catch {};
    sb_text.append("    recipient@example.com\r\n\r\n") catch {};
    sb_text.append("Technical details of permanent failure:\r\n") catch {};
    sb_text.append("DNS Error: Domain name not found\r\n") catch {};

    const explain = try sb_text.getAsString(alloc);

    const dsn_email = try chilkat.Email.init();
    defer dsn_email.deinit();
    email.toDsn(explain, try xml.getXml(alloc), header_only, dsn_email) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try dsn_email.getMime(alloc)});

    // Here's the MIME of the DNS email we just created:
    // -------------------------------------------------

    // Content-Type: multipart/report; report-type="delivery-status"; boundary="------------060100020300020303000802"
    //
    // --------------060100020300020303000802
    // Content-Type: text/plain; charset=windows-1252; format=flowed
    // Content-Transfer-Encoding: 7bit
    //
    // This is an automatically generated Delivery Status Notification.
    //
    // Delivery to the following recipient failed permanently:
    //
    //     recipient@example.com
    //
    // Technical details of permanent failure:
    // DNS Error: Domain name not found
    //
    // --------------060100020300020303000802
    // Content-Type: message/delivery-status
    //
    // Final-Recipient: rfc822; recipient@example.com
    // Action: failed
    // Status: 5.1.2
    // Diagnostic-Code: smtp; 550 5.1.2 Host unknown (Domain name not found)
    // Last-Attempt-Date: Mon, 12 May 2025 11:30:39 -0500
    //
    // --------------060100020300020303000802
    // Content-Type: text/rfc822-headers; charset=windows-1252
    //
    // MIME-Version: 1.0
    // Date: Mon, 12 May 2025 11:30:39 -0500
    // Message-ID: <B8E6875D582A78AE779FC0B46ACC8C858CEAF608@SLICE>
    // Content-Type: text/plain; charset=us-ascii; format=flowed
    // Content-Transfer-Encoding: 7bit
    // X-Priority: 3 (Normal)
    // Subject: Test
    // From: joe@example.com
    // To: recipient@example.com
    // --------------060100020300020303000802--
}