← Back to Official Package Registry
Tier 1: Backend Essentials • Transactional Email & DKIMv1.0.0 • 39 KB • Ed25519 Verified

smtp_mail@1.0.0

Multipart MIME composer, Ed25519 RFC 8463 DKIM signer/verifier, SMTP STARTTLS/TLS client, and Resend/Postmark payloads.

use [smtp_mail]
veko pkg add smtp_mail@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

smtp_mail

Pure-Veko RFC 5322 / RFC 2045 Multipart MIME composer, RFC 6376 / RFC 8463 Ed25519-SHA256 DKIM signer and verifier, RFC 5321 SMTP client (io/Stream and TLS 1.3), and Resend/Postmark HTTPS API payload builders.

Overview

smtp_mail provides transactional and outbound email infrastructure in pure Veko with zero external C or OpenSSL dependencies:

  • Multipart MIME (encode-mime): Composes RFC 5322 headers (From, To, Cc, Date, Message-ID, MIME-Version) with automatic RFC 2047 UTF-8 Base64 subject encoding (=?UTF-8?B?...?=), multipart/alternative (text/plain + text/html), and multipart/mixed 76-column Base64 binary attachments.
  • Ed25519-SHA256 DKIM (sign-dkim-ed25519, verify-dkim-ed25519): Implements RFC 6376 relaxed/relaxed header and body canonicalization paired with RFC 8463 ed25519-sha256 signatures (crypto/sha256 + crypto/x25519) and DNS TXT _domainkey record generation.
  • Transport-Agnostic SMTP Client (send-stream, send-tls): Drives the full RFC 5321 state machine (220 -> EHLO -> AUTH PLAIN -> MAIL FROM -> RCPT TO -> DATA with RFC 5321 Section 4.5.2 dot-stuffing -> QUIT) over any io/Stream or implicit TLS 1.3 (net/tls13).
  • HTTPS Provider Bridges (to-resend-json, to-postmark-json): Formats Email messages directly into Resend and Postmark REST API JSON payloads for serverless or port-restricted environments.

Installation

powershell
veko pkg add smtp_mail@1.0.0

Import in .vk modules:

rebol
use [mail = smtp_mail]

Architecture & Security Notes

  1. CRLF Header Injection Defense: Every header field name, recipient address, attachment filename, and subject passed into encode-mime or send-stream is scrubbed via sanitize-header to strip \r, \n, and NUL bytes before serialization.
  2. Constant-Time Body Hash & Ed25519 Verification: verify-dkim-ed25519 compares the canonicalized body SHA-256 (bh=) using crypto/sha256/ct-eq before verifying the 64-byte RFC 8032 Ed25519 signature over SHA-256(canonical_headers).
  3. Bcc Privacy: Email.bcc recipients are included in RCPT TO SMTP envelope commands (and HTTPS provider JSON fields) while never being serialized into the visible encode-mime message headers.
  4. RFC 5321 Transparency Dot-Stuffing: dot-stuff prefixes any body line beginning with . with an extra . before appending \r\n.\r\n, preventing premature DATA termination or SMTP smuggling.

Complete API Reference

Data Structures

TypeFieldsDescription
Attachmentfilename str, content_type str = 'application/octet-stream', data bytesBinary or text file attachment.
Emailfrom str, to block[str], cc block[str] = [], bcc block[str] = [], subject str, text_body str = '', html_body str = '', headers map[str] = {}, attachments block = []Complete email message specification.

Functions

SignatureDescription
sanitize-header = fn [s str] strStrips \r, \n, and \0 to prevent header injection.
encode-subject-rfc2047 = fn [subject str] strEncodes non-ASCII subjects as =?UTF-8?B?...?=.
extract-envelope-addr = fn [addr str] strExtracts user@domain from 'Name <user@domain>'.
encode-mime = fn [email Email /at unix_sec i64 = -1] strSerializes email into RFC 5322 / RFC 2045 CRLF MIME text.
canonicalize-body-relaxed = fn [body str] strApplies RFC 6376 Section 3.4.4 relaxed body canonicalization.
canonicalize-header-relaxed = fn [name str, value str] strApplies RFC 6376 Section 3.4.2 relaxed header canonicalization.
dkim-dns-record = fn [selector str, domain str, ed25519_pubkey bytes] map[str]Builds the <selector>._domainkey.<domain> DNS TXT record.
sign-dkim-ed25519 = fn [mime_msg str, domain str, selector str, ed25519_seed bytes /at unix_sec i64 = -1] strSigns mime_msg and prepends DKIM-Signature: v=1; a=ed25519-sha256; ....
verify-dkim-ed25519 = fn [signed_mime str, ed25519_pubkey bytes] boolVerifies both bh= body SHA-256 and Ed25519 header signature.
dot-stuff = fn [mime_str str] strApplies RFC 5321 Section 4.5.2 dot-stuffing and appends \r\n.\r\n.
send-stream = fn [mut stream io/Stream, helo_host str, auth_user str, auth_pass str, email Email /raw raw_msg str = ''] mapDelivers email (or pre-signed raw_msg) over any io/Stream.
send-tls = fn [host str, port i64, helo_host str, auth_user str, auth_pass str, email Email /raw raw_msg str = ''] mapConnects over net/tls13 (port 465 SMTPS) and delivers email.
to-resend-json = fn [email Email] str / to-postmark-json = fn [email Email] strBuilds JSON request bodies for Resend and Postmark HTTPS APIs.

Idiomatic Veko Examples

Composing, DKIM-Signing (Ed25519), and Sending over SMTPS

rebol
use [mail = smtp_mail, hex = codec/hex]

email = mail/Email {
    from        = 'Veko Auth <no-reply@veko-lang.org>'
    to          = ['user@example.com']
    subject     = 'Your security verification link'
    text_body   = 'Open https://veko-lang.org/verify to continue.\n'
    html_body   = '<p>Open <a href="https://veko-lang.org/verify">Verify Account</a>.</p>\n'
    attachments = [
        (mail/Attachment {
            filename     = 'receipt.txt'
            content_type = 'text/plain'
            data         = (bytes/from-str 'Receipt #4092\n')
        })
    ]
}

mime = mail/encode-mime email
seed = hex/decode (os/getenv/or 'DKIM_ED25519_SEED_HEX' ['0000000000000000000000000000000000000000000000000000000000000000'])
signed = mail/sign-dkim-ed25519 mime 'veko-lang.org' 's2026' seed

// Deliver via implicit TLS 1.3 (port 465)
res = mail/send-tls/raw/or 'smtp.example.com' 465 'app.veko-lang.org' 'smtp-user' 'smtp-pass' email signed [e ->
    println `SMTP delivery failed: {mold e}`
    return e
]
println `Queued for {res.recipients} recipient(s)`

Verification

powershell
veko check packages/smtp_mail/src/mod.vk
veko check packages/smtp_mail/examples/demo.vk
veko run -C all packages/smtp_mail/examples/demo.vk
packages/smtp_mail/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/smtp_mail/examples/demo.vk
// Runnable verification suite for `smtp_mail` (Multipart MIME, RFC 8463 Ed25519 DKIM, and loopback SMTP).

use [
	hex  = codec/hex
	io
	inet = io/net
	mail = smtp_mail
	text
	x25  = crypto/x25519
]

assert-eq = fn [actual any, expected any, label str] any [
	if actual != expected [
		return (err (str/join ['ASSERT FAILED [' label ']: got ' (mold actual) ', expected ' (mold expected)] ''))
	]
	.ok
]

assert-true = fn [cond bool, label str] any [
	if not cond [
		return (err (str/join ['ASSERT FAILED [' label ']'] ''))
	]
	.ok
]

LoopBuf = struct {
	buf bytes
}

read-crlf-line = fn [mut st io/Stream, mut lb LoopBuf] str [
	mut acc = lb.buf
	while true [
		mut nl = -1
		mut i = 0
		while i < (len acc) [
			if acc[i] == 10 [
				nl = i
				break
			]
			i = i + 1
		]
		if nl >= 0 [
			mut le = nl
			if le > 0 and acc[le - 1] == 13 [ le = le - 1 ]
			s = bytes/to-str acc[0..le]
			put mut lb .buf acc[(nl + 1)..(len acc)]
			return s
		]
		ch = io/read/or mut st 1024 [ bytes/with-cap 0 ]
		if (len ch) == 0 [
			put mut lb .buf acc
			return ''
		]
		_ = bytes/push-all mut acc ch
	]
	''
]

run-loopback-smtp-server = fn [ln any, want_auth_b64 str] map [
	sock = net/accept ln
	mut st = inet/tcp-stream (as-i64 sock) 'smtp-client'
	defer [
		_ = io/close/or mut st [.ok]
		_ = net/close/or ln [.ok]
	]

	mut rbuf = LoopBuf { buf = (bytes/with-cap 1024) }
	_ = io/write-all mut st (bytes/from-str '220 mx.veko-lang.org ESMTP VekoTest\r\n')

	mut authed = false
	mut mail_from = ''
	mut rcpts = []
	mut data_lines = []
	mut running = true

	while running [
		cmd = read-crlf-line mut st mut rbuf
		if cmd == '' [
			running = false
		] else [
			up = str/upper cmd
			if text/starts-with? up 'EHLO ' [
				_ = io/write-all mut st (bytes/from-str '250-mx.veko-lang.org Hello\r\n250-8BITMIME\r\n250 AUTH PLAIN\r\n')
			] else [
			if text/starts-with? up 'AUTH PLAIN ' [
				tok = str/trim cmd[11..(len cmd)]
				if tok == want_auth_b64 [
					authed = true
					_ = io/write-all mut st (bytes/from-str '235 2.7.0 Authentication successful\r\n')
				] else [
					_ = io/write-all mut st (bytes/from-str '535 5.7.8 Invalid credentials\r\n')
				]
			] else [
			if text/starts-with? up 'MAIL FROM:' [
				mail_from = str/trim cmd[10..(len cmd)]
				_ = io/write-all mut st (bytes/from-str '250 2.1.0 Originator OK\r\n')
			] else [
			if text/starts-with? up 'RCPT TO:' [
				push mut rcpts (str/trim cmd[8..(len cmd)])
				_ = io/write-all mut st (bytes/from-str '250 2.1.5 Recipient OK\r\n')
			] else [
			if up == 'DATA' [
				_ = io/write-all mut st (bytes/from-str '354 End data with <CR><LF>.<CR><LF>\r\n')
				mut reading_data = true
				while reading_data [
					dln = read-crlf-line mut st mut rbuf
					if dln == '.' [
						reading_data = false
					] else [
						// RFC 5321 §4.5.2 un-dot-stuffing
						unstuffed = if (len dln) >= 2 and dln[0..2] == '..' [ dln[1..(len dln)] ] else [ dln ]
						push mut data_lines unstuffed
					]
				]
				_ = io/write-all mut st (bytes/from-str '250 2.0.0 Queued as <veko-q-001>\r\n')
			] else [
			if up == 'QUIT' [
				_ = io/write-all mut st (bytes/from-str '221 2.0.0 Bye\r\n')
				running = false
			] else [
				_ = io/write-all mut st (bytes/from-str '500 Unrecognized command\r\n')
			]]]]]]
		]
	]

	{
		authed       = authed
		mail_from    = mail_from
		rcpt_count   = len rcpts
		received_msg = str/append (str/join data_lines '\r\n') '\r\n'
	}
]

main = fn [] any [
	println '=== smtp_mail 1.0.0 Verification Suite ==='

	// 1. Multipart MIME (text + HTML + Base64 attachment + RFC 2047 UTF-8 subject)
	utf8_subj = str/join ['Veko 1.0 Release ' (text/utf8-from-codepoints [0x2014]) ' Ready'] ''
	att_bytes = bytes/from-str 'SHA256 (veko-1.0.0.zip) = ba7816bf8f01cfea\n'

	msg = mail/Email {
		from        = 'Veko Releases <releases@veko-lang.org>'
		to          = ['Ada <ada@example.org>' 'lin@example.org']
		cc          = ['ops@example.org']
		subject     = utf8_subj
		text_body   = 'Hello from Veko!\n.dot-prefixed-line-preserved\nRegards,\nVeko Team\n'
		html_body   = '<h1>Hello from Veko!</h1><p>Deterministic single-owner runtime.</p>\n'
		headers     = { 'X-Veko-Track' = 'all-three' }
		attachments = [
			(mail/Attachment {
				filename     = 'SHA256SUMS.txt'
				content_type = 'text/plain'
				data         = att_bytes
			})
		]
	}

	raw_mime = mail/encode-mime/at msg 1764892800
	if err? raw_mime [ return raw_mime ]
	r1 = assert-true (str/contains? raw_mime 'Subject: =?UTF-8?B?') 'RFC 2047 UTF-8 Base64 subject'
	if err? r1 [ return r1 ]
	r2 = assert-true (str/contains? raw_mime 'multipart/mixed;') 'outer multipart/mixed'
	if err? r2 [ return r2 ]
	r3 = assert-true (str/contains? raw_mime 'multipart/alternative;') 'nested multipart/alternative'
	if err? r3 [ return r3 ]
	r4 = assert-true (str/contains? raw_mime 'filename="SHA256SUMS.txt"') 'attachment Content-Disposition'
	if err? r4 [ return r4 ]
	println '  [PASS] Multipart MIME (mixed + alternative + Base64 attachment + RFC 2047 subject)'

	// 2. RFC 6376 / RFC 8463 Ed25519-SHA256 DKIM signing, verification & tamper detection
	dkim_seed = hex/decode '9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60'
	dkim_pub  = x25/ed25519-pubkey dkim_seed
	dns_rec   = mail/dkim-dns-record 's2026' 'veko-lang.org' dkim_pub
	r5 = assert-eq (map/get dns_rec 'name') 's2026._domainkey.veko-lang.org' 'DKIM DNS record name'
	if err? r5 [ return r5 ]
	r6 = assert-true (str/contains? (map/get dns_rec 'value') 'v=DKIM1; k=ed25519; p=') 'DKIM DNS record TXT value'
	if err? r6 [ return r6 ]

	signed_mime = mail/sign-dkim-ed25519/at raw_mime 'veko-lang.org' 's2026' dkim_seed 1764892800
	if err? signed_mime [ return signed_mime ]
	r7 = assert-true (text/starts-with? signed_mime 'DKIM-Signature: v=1; a=ed25519-sha256;') 'DKIM-Signature header prepended'
	if err? r7 [ return r7 ]
	r8 = assert-true (mail/verify-dkim-ed25519 signed_mime dkim_pub) 'DKIM Ed25519 signature valid'
	if err? r8 [ return r8 ]

	tampered_body = text/replace signed_mime 'Hello from Veko!' 'Hello from Attacker!'
	r9 = assert-eq (mail/verify-dkim-ed25519 tampered_body dkim_pub) false 'DKIM rejects tampered body'
	if err? r9 [ return r9 ]

	tampered_hdr = text/replace signed_mime 'To: Ada <ada@example.org>' 'To: Eve <eve@example.org>'
	r10 = assert-eq (mail/verify-dkim-ed25519 tampered_hdr dkim_pub) false 'DKIM rejects tampered signed header'
	if err? r10 [ return r10 ]
	println '  [PASS] RFC 8463 Ed25519-SHA256 DKIM signing, verification & tamper rejection'

	// 3. Resend & Postmark HTTPS API JSON builders
	resend_j = json/decode (mail/to-resend-json msg)
	postmark_j = json/decode (mail/to-postmark-json msg)
	r11 = assert-eq (len (map/get resend_j 'attachments')) 1 'Resend JSON attachment count'
	if err? r11 [ return r11 ]
	r12 = assert-eq (map/get postmark_j 'Cc') 'ops@example.org' 'Postmark JSON Cc field'
	if err? r12 [ return r12 ]
	println '  [PASS] Resend & Postmark HTTPS API JSON payload builders'

	// 4. Full loopback SMTP protocol state machine (`EHLO`, `AUTH PLAIN`, `MAIL FROM`, `RCPT TO`, `DATA` dot-stuffing, `QUIT`)
	ln = net/listen-tcp .ip '127.0.0.1:0'
	port_num = str/to-i64/or (str/split (net/addr ln) ':')[1] [ 0 ]

	// Expected SASL PLAIN Base64 for `\0mailer\0s3cret-pass`
	mut sasl = bytes/with-cap 20
	_ = bytes/push mut sasl 0
	_ = bytes/push-all mut sasl (bytes/from-str 'mailer')
	_ = bytes/push mut sasl 0
	_ = bytes/push-all mut sasl (bytes/from-str 's3cret-pass')
	want_b64 = hex/b64-encode sasl

	srv_task = spawn [
		run-loopback-smtp-server ln want_b64
	]

	mut client_st = inet/dial-tcp '127.0.0.1' port_num
	send_res = mail/send-stream/raw mut client_st 'app.veko-lang.org' 'mailer' 's3cret-pass' msg signed_mime
	_ = io/close/or mut client_st [.ok]
	if err? send_res [ return send_res ]

	srv_res = task/await srv_task
	r13 = assert-eq send_res.ok true 'SMTP client session succeeded'
	if err? r13 [ return r13 ]
	r14 = assert-eq send_res.recipients 3 'SMTP envelope recipient count (2 To + 1 Cc)'
	if err? r14 [ return r14 ]
	r15 = assert-eq srv_res.authed true 'SMTP server verified AUTH PLAIN'
	if err? r15 [ return r15 ]
	r16 = assert-eq srv_res.mail_from '<releases@veko-lang.org>' 'SMTP server received bare MAIL FROM'
	if err? r16 [ return r16 ]
	r17 = assert-true (str/contains? srv_res.received_msg '\r\n.dot-prefixed-line-preserved\r\n') 'RFC 5321 dot-stuffing & un-stuffing roundtrip'
	if err? r17 [ return r17 ]
	r18 = assert-true (mail/verify-dkim-ed25519 srv_res.received_msg dkim_pub) 'DKIM signature still valid after SMTP DATA transport!'
	if err? r18 [ return r18 ]

	println '  [PASS] Live loopback SMTP session (EHLO, AUTH PLAIN, RCPT, dot-stuffing & DKIM intact)'
	println 'ALL smtp_mail TESTS PASSED'
	.ok
]

res = main
if err? res [
	println (mold res)
	os/exit 1
]
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/smtp_mail/1.0.0.vkp
Archive SHA-256: 6290d454bfbe06bd5098fc6d4b641bc5e11737a4fc5fc034aabf605cf077f6b7
Merkle Root: 7944e6baa00446d1b8b15bccdd93332929b7afcc74f0e357e3ca9ad0e5546a1e
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"smtp_mail","version":"1.0.0","tier":1.0,"tier_label":"Tier 1: Backend Essentials","category":"Transactional Email & DKIM","description":"Multipart MIME composer, Ed25519 RFC 8463 DKIM signer/verifier, SMTP STARTTLS/TLS client, and Resend/Postmark payloads.","modules":["smtp_mail"],"homepage":"https://veko-lang.org/packages/smtp_mail"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md6 KB2e6e7d8714cb0cc7f9d81c23628aa747515a46d5537e1a7cae0e9cffc8187e8c
examples/demo.vk8 KB946e3a96ed3078e07005f48ddf6fe6768a14a1ba925e57ce1418df3fc06d5309
mod.vk24 KB99920dec2347239d72f7504d9043b7022841ed1d768a30fc9665af1445b0f913