Skip to content

Commit 03fa71e

Browse files
committed
chore: update message id formatting
1 parent 5d02fc7 commit 03fa71e

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/authentication.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub const Credentials = struct {
2424
/// Encodes these credentials based of the auth offerings from the server.
2525
///
2626
/// This writes to the connection directly.
27+
/// Assumes that the writers buffer has enough room to fix the base64 encoded encoded auth.
2728
pub fn encode(self: Credentials, auth: Auth, connection: *Connection) Error!void {
2829
const reader = connection.reader();
2930
const writer = connection.writer();
@@ -56,9 +57,9 @@ pub const Credentials = struct {
5657
var buffer: [512]u8 = undefined;
5758
const plain = try std.fmt.bufPrint(&buffer, "\x00{s}\x00{s}", .{ self.username, self.password });
5859

59-
std.debug.assert(writer.buffer.len > std.base64.standard.Encoder.calcSize(plain.len)); // Cannot fit into writers buffer
60+
const writable = try writer.writableSliceGreedy(std.base64.standard.Encoder.calcSize(plain.len));
6061

61-
const encoded = std.base64.standard.Encoder.encode(writer.buffer[writer.end..], plain);
62+
const encoded = std.base64.standard.Encoder.encode(writable, plain);
6263
writer.advance(encoded.len);
6364

6465
try writer.writeAll("\r\n");

src/message.zig

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub const SingleMessageBody = union(enum) {
3232
text: []const u8,
3333
/// Writes the Content-Type as "text/html".
3434
html: []const u8,
35-
/// Data structure that represent as email attachment.
35+
/// Data structure that represents as email attachment.
3636
attachment: Attachment,
3737

3838
/// Formats the email message body into the expected header and content values.
@@ -140,9 +140,7 @@ pub const MultipartMessageBody = union(enum) {
140140

141141
try writer.writeAll("\r\n");
142142
}
143-
}
144-
145-
if (body.text) |text_body| {
143+
} else if (body.text) |text_body| {
146144
try writer.writeAll("Content-Type: text/plain; charset=utf-8\r\n");
147145
try writer.writeAll("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
148146
try quotable_printable.encodeWriter(writer, text_body);
@@ -180,7 +178,7 @@ pub const MultipartMessageBody = union(enum) {
180178
try writer.writeAll("\r\n");
181179

182180
for (body.attachments) |attachment| {
183-
std.debug.assert(attachment == .inlined); // Cannot send inlined email attachment in multipart/related
181+
std.debug.assert(attachment == .inlined); // Cannot send non inlined email attachment in multipart/related
184182
try writer.print("--{x}\r\n", .{&boundary});
185183
try writer.print("{f}", .{attachment});
186184
}
@@ -212,9 +210,17 @@ pub const MessageBody = union(enum) {
212210
/// Data structure that represent an email attachment that
213211
/// can either be inlined or sent just as the attachment.
214212
pub const Attachment = union(enum) {
215-
/// The attachment can be inlined in the email body.
213+
/// The attachment can be inlined in the email body if
214+
/// the email body is a html body.
215+
///
216+
/// Example:
217+
///
218+
/// ```zig
219+
/// const content_id = try std.fmt.bufPrint(&buffer, "{x}@{s}", .{ message_id.id, message_id.domain });
220+
/// const html = try std.fmt.bufPrint(&buffer1, "<img src=\"cid:{s}\" alt=\"Fooo\" />", .{content_id});
221+
/// ```
216222
inlined: struct {
217-
content_id: []const u8,
223+
content_id: MessageId,
218224
body_contents: []const u8,
219225
content_type: []const u8,
220226
name: ?[]const u8,
@@ -245,7 +251,7 @@ pub const Attachment = union(enum) {
245251
try writer.writeAll("Content-Transfer-Encoding: base64\r\n");
246252
try writer.print("Content-Disposition: inline; filename={s}\r\n", .{name});
247253
try writer.print("Content-Location: {s};\r\n", .{name});
248-
try writer.print("Content-Id: <{s}>\r\n\r\n", .{content.content_id});
254+
try writer.print("Content-Id: {f}\r\n\r\n", .{content.content_id});
249255
try writer.print("{b64}\r\n", .{content.body_contents});
250256
} else {
251257
try writer.print("Content-Type: {s};\r\n", .{content.content_type});

0 commit comments

Comments
 (0)