Something went wrong. Try again.
Reading, writing and creating MIME and RFC 5322 email messages in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! What media type to give a file, guessed from the end of its name.//!//! A `Content-Type` is the only thing telling the receiving program what an//! attachment *is*. Get it wrong and a PDF arrives as//! `application/octet-stream`: no preview, no icon, no "open with", and on//! some clients a warning about an unknown file. The sender knew perfectly//! well what it was, and the information was thrown away at the last moment//! because naming the type was one more argument to pass.//!//! So this exists to make the right answer the default. It is a table of the//! extensions that actually turn up in mail rather than a copy of the IANA//! registry, which has some two thousand entries and would be most of this//! library by weight. Everything it does not know is//! `application/octet-stream`, which is the correct thing to say when you do//! not know — RFC 2046 §4.5.1 — and which is what the caller would have had to//! write by hand anyway.//!//! Guessing from a filename is a heuristic and is documented as one. Where the//! type is actually known, pass it: `DataOptions.content_type` always wins.
const std = @import("std");const testing = std.testing;
/// What an unknown file is. RFC 2046 §4.5.1: the type that means "these are/// bytes and I am not going to pretend to know more than that".pub const default = "application/octet-stream";
/// Extension, without the dot, to media type. Lowercase; the lookup folds/// case.////// Ordered roughly by how often each turns up on a message, which matters only/// because the lookup is a linear scan — ninety entries searched once per/// attachment is not worth a hash table, and a table that can be read top to/// bottom is worth something.const table = [_]struct { []const u8, []const u8 }{ // Documents, which are most of what is ever attached. .{ "pdf", "application/pdf" }, .{ "doc", "application/msword" }, .{ "docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }, .{ "xls", "application/vnd.ms-excel" }, .{ "xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, .{ "ppt", "application/vnd.ms-powerpoint" }, .{ "pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" }, .{ "odt", "application/vnd.oasis.opendocument.text" }, .{ "ods", "application/vnd.oasis.opendocument.spreadsheet" }, .{ "odp", "application/vnd.oasis.opendocument.presentation" }, .{ "rtf", "application/rtf" }, .{ "epub", "application/epub+zip" },
// Text. .{ "txt", "text/plain" }, .{ "text", "text/plain" }, .{ "log", "text/plain" }, .{ "md", "text/markdown" }, .{ "markdown", "text/markdown" }, .{ "html", "text/html" }, .{ "htm", "text/html" }, .{ "css", "text/css" }, .{ "csv", "text/csv" }, .{ "tsv", "text/tab-separated-values" }, .{ "ics", "text/calendar" }, .{ "vcf", "text/vcard" }, .{ "xml", "text/xml" }, .{ "diff", "text/x-diff" }, .{ "patch", "text/x-diff" },
// Images, which are the other half of what is attached and all of what is // embedded in an HTML body. .{ "png", "image/png" }, .{ "jpg", "image/jpeg" }, .{ "jpeg", "image/jpeg" }, .{ "jpe", "image/jpeg" }, .{ "gif", "image/gif" }, .{ "webp", "image/webp" }, .{ "svg", "image/svg+xml" }, .{ "bmp", "image/bmp" }, .{ "tif", "image/tiff" }, .{ "tiff", "image/tiff" }, .{ "ico", "image/vnd.microsoft.icon" }, .{ "heic", "image/heic" }, .{ "heif", "image/heif" }, .{ "avif", "image/avif" },
// Archives. .{ "zip", "application/zip" }, .{ "gz", "application/gzip" }, .{ "tgz", "application/gzip" }, .{ "bz2", "application/x-bzip2" }, .{ "xz", "application/x-xz" }, .{ "zst", "application/zstd" }, .{ "tar", "application/x-tar" }, .{ "7z", "application/x-7z-compressed" }, .{ "rar", "application/vnd.rar" },
// Data and code. .{ "json", "application/json" }, .{ "jsonl", "application/jsonl" }, .{ "yaml", "application/yaml" }, .{ "yml", "application/yaml" }, .{ "toml", "application/toml" }, .{ "js", "text/javascript" }, .{ "mjs", "text/javascript" }, .{ "wasm", "application/wasm" }, .{ "sqlite", "application/vnd.sqlite3" }, .{ "db", "application/vnd.sqlite3" },
// Audio. .{ "mp3", "audio/mpeg" }, .{ "m4a", "audio/mp4" }, .{ "aac", "audio/aac" }, .{ "oga", "audio/ogg" }, .{ "ogg", "audio/ogg" }, .{ "opus", "audio/ogg" }, .{ "flac", "audio/flac" }, .{ "wav", "audio/wav" }, .{ "amr", "audio/amr" },
// Video. .{ "mp4", "video/mp4" }, .{ "m4v", "video/mp4" }, .{ "webm", "video/webm" }, .{ "ogv", "video/ogg" }, .{ "mov", "video/quicktime" }, .{ "avi", "video/x-msvideo" }, .{ "mkv", "video/x-matroska" }, .{ "mpg", "video/mpeg" }, .{ "mpeg", "video/mpeg" },
// Fonts. .{ "woff", "font/woff" }, .{ "woff2", "font/woff2" }, .{ "ttf", "font/ttf" }, .{ "otf", "font/otf" },
// Mail's own, which is how a forwarded message and a signature arrive. .{ "eml", "message/rfc822" }, .{ "mbox", "application/mbox" }, .{ "asc", "application/pgp-signature" }, .{ "sig", "application/pgp-signature" }, .{ "pgp", "application/pgp-encrypted" }, .{ "p7s", "application/pkcs7-signature" }, .{ "p7m", "application/pkcs7-mime" }, .{ "crt", "application/x-x509-ca-cert" }, .{ "pem", "application/x-pem-file" },};
/// The media type for an extension written without its dot, or null.pub fn fromExtension(extension: []const u8) ?[]const u8 { if (extension.len == 0 or extension.len > 16) return null; for (table) |entry| { if (std.ascii.eqlIgnoreCase(extension, entry[0])) return entry[1]; } return null;}
/// The media type for a filename, or null if its extension is not one this/// knows — or if it has no extension at all, which a name ending in a dot and/// a dotfile such as `.bashrc` both count as.pub fn fromFilename(filename: []const u8) ?[]const u8 { const dot = std.mem.lastIndexOfScalar(u8, filename, '.') orelse return null; if (dot == 0) return null; // A dotfile is not an extension. return fromExtension(filename[dot + 1 ..]);}
/// The media type for a filename, falling back to `default`. What/// `Part.setData` uses when it is not told a type.pub fn forFilename(filename: ?[]const u8) []const u8 { const name = filename orelse return default; return fromFilename(name) orelse default;}
test fromExtension { try testing.expectEqualStrings("application/pdf", fromExtension("pdf").?); try testing.expectEqualStrings("application/pdf", fromExtension("PDF").?); try testing.expectEqualStrings("image/jpeg", fromExtension("jpeg").?); try testing.expectEqual(null, fromExtension("wibble")); try testing.expectEqual(null, fromExtension(""));}
test fromFilename { try testing.expectEqualStrings("application/pdf", fromFilename("report.pdf").?); try testing.expectEqualStrings("image/png", fromFilename("Logo.PNG").?); try testing.expectEqualStrings("application/gzip", fromFilename("archive.tar.gz").?); try testing.expectEqualStrings("text/csv", fromFilename("Quartalsbericht für München.csv").?); // No extension to read. try testing.expectEqual(null, fromFilename("README")); try testing.expectEqual(null, fromFilename("archive.")); try testing.expectEqual(null, fromFilename(".bashrc")); try testing.expectEqual(null, fromFilename(""));}
test forFilename { try testing.expectEqualStrings("application/pdf", forFilename("report.pdf")); try testing.expectEqualStrings(default, forFilename("README")); try testing.expectEqualStrings(default, forFilename(null));}
test "every entry is a well-formed media type" { for (table) |entry| { try testing.expect(entry[0].len != 0); for (entry[0]) |c| try testing.expect(std.ascii.isLower(c) or std.ascii.isDigit(c)); const slash = std.mem.findScalar(u8, entry[1], '/') orelse return error.NoSubtype; try testing.expect(slash != 0); try testing.expect(slash != entry[1].len - 1); }}
test "no extension is listed twice" { for (table, 0..) |entry, i| { for (table[i + 1 ..]) |other| { if (std.mem.eql(u8, entry[0], other[0])) { std.debug.print("duplicate extension: {s}\n", .{entry[0]}); return error.DuplicateExtension; } } }}