Skip to content

net.mail #

net.mail builds and parses MIME messages for use with transports such as net.smtp. Create a message with new_message(), set from, recipients, subject, and text/HTML bodies, then attach files with add_attachment(filename, data, mime_type) and call encode(). Encoding uses CRLF endings, multipart/alternative for text plus HTML, and multipart/mixed when attachments are present, nesting the text/HTML alternatives inside it. Every attachment uses base64, including text attachments. Filenames are quoted and escaped. Header CR/LF/NUL characters, invalid boundaries, and boundary lines present in text bodies return errors. Use set_boundary() to choose another delimiter.

parse(raw) supports empty or folded headers, nested multiparts, text and HTML bodies, and base64, 7bit, 8bit, or binary attachments. Delimiters match complete lines; preamble and epilogue are ignored. Attachment bytes are preserved. Body text is normalized to LF; a single final line ending on a plain single-part body is omitted. Multipart body text and decoded base64 text retain their payload line endings after normalization. Missing closing delimiters, malformed base64, unsupported transfer encodings, and nesting beyond 32 levels return errors. Quoted-printable and RFC 2047 encoded words are not decoded; header values pass through unchanged. Recipient lists use comma-separated addresses and do not implement quoted display-name address parsing.

fn new_message #

fn new_message() Message

new_message creates an empty Message.

fn parse #

fn parse(raw string) !Message

parse reads RFC 5322 text back into a Message: headers (with folding), text/html bodies, and base64 or 7bit attachments. Quoted-printable parts are an error (no decoder in vlib); header values pass through verbatim.

struct Attachment #

struct Attachment {
pub:
	filename  string
	mime_type string = 'application/octet-stream'
	data      []u8
}

Attachment is one file carried by a Message.

struct Message #

struct Message {
pub mut:
	from        string
	to          []string
	cc          []string
	subject     string
	text_body   string
	html_body   string
	attachments []Attachment
	boundary    string = default_boundary
}

Message is an email message: headers plus text/html bodies and attachments. Build one with the setters and encode, or read one back with parse. It is the message layer that net.smtp (send) and net.imap/net.pop (fetch) transport as opaque RFC 5322 text.

fn (Message) set_from #

fn (mut m Message) set_from(addr string)

set_from sets the From header.

fn (Message) add_to #

fn (mut m Message) add_to(addr string)

add_to appends a To recipient.

fn (Message) add_cc #

fn (mut m Message) add_cc(addr string)

add_cc appends a Cc recipient.

fn (Message) set_subject #

fn (mut m Message) set_subject(subject string)

set_subject sets the Subject header (ASCII; RFC 2047 encoding is out of scope and passed through verbatim).

fn (Message) set_text_body #

fn (mut m Message) set_text_body(body string)

set_text_body sets the plain-text body.

fn (Message) set_html_body #

fn (mut m Message) set_html_body(body string)

set_html_body sets the HTML body variant.

fn (Message) add_attachment #

fn (mut m Message) add_attachment(filename string, data []u8, mime_type string)

add_attachment attaches data under filename with a MIME type ('application/octet-stream' when empty).

fn (Message) set_boundary #

fn (mut m Message) set_boundary(boundary string)

set_boundary overrides the MIME part boundary.

fn (Message) encode #

fn (m &Message) encode() !string

encode renders the message as RFC 5322 text: plain text/plain when there is only a text body, multipart/alternative for text+html, and multipart/mixed once attachments are present.