diff --git a/src/Email.zig b/src/Email.zig index ab22070..c03ed67 100644 --- a/src/Email.zig +++ b/src/Email.zig @@ -17,7 +17,10 @@ pub fn deinit(self: Self) void { /// Initializes gmime if not already initialized fn gmimeInit(self: *Self) void { - if (!self.initialized) gmime.g_mime_init(); + if (!self.initialized) { + gmime.g_mime_init(); + self.initialized = true; + } } pub fn openMessage(self: *Self, filename: [:0]const u8) !Message { @@ -112,7 +115,7 @@ pub const Message = struct { fn findHtmlInMultipart(multipart: *gmime.GMimeMultipart, allocator: std.mem.Allocator) !?[]const u8 { const count = gmime.g_mime_multipart_get_count(multipart); - // Look for HTML part + // Look for HTML part first (preferred in multipart/alternative) var i: usize = 0; while (i < count) : (i += 1) { const part = gmime.g_mime_multipart_get_part(multipart, @intCast(i)); @@ -131,7 +134,6 @@ pub const Message = struct { if (std.mem.eql(u8, std.mem.span(part_mime_type), "text") and std.mem.eql(u8, std.mem.span(part_mime_subtype), "html")) { - // Try to get the text content if (gmime.g_type_check_instance_is_a(@as(*gmime.GTypeInstance, @ptrCast(part)), gmime.g_mime_text_part_get_type()) != 0) { const text_part = @as(*gmime.GMimeTextPart, @ptrCast(part)); @@ -141,8 +143,15 @@ pub const Message = struct { } } } - // Check if this is another multipart container (for nested multiparts) - else if (gmime.g_type_check_instance_is_a(@as(*gmime.GTypeInstance, @ptrCast(part)), gmime.g_mime_multipart_get_type()) != 0) { + } + + // If no HTML found, check for nested multiparts (like multipart/related inside multipart/alternative) + i = 0; + while (i < count) : (i += 1) { + const part = gmime.g_mime_multipart_get_part(multipart, @intCast(i)); + if (part == null) continue; + + if (gmime.g_type_check_instance_is_a(@as(*gmime.GTypeInstance, @ptrCast(part)), gmime.g_mime_multipart_get_type()) != 0) { const nested_multipart = @as(*gmime.GMimeMultipart, @ptrCast(part)); if (try findHtmlInMultipart(nested_multipart, allocator)) |content| { return content; @@ -153,18 +162,42 @@ pub const Message = struct { return null; } - pub fn rawBody(_: Message, allocator: std.mem.Allocator) ![]const u8 { - // For the test cases, we know exactly what HTML content we need to return - // This is a simplified implementation that directly returns the expected HTML - const html_content = - \\ - \\
- \\ - \\ - \\