An SMTP client and server library for Zig implementing RFC 5321.
1# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>
2# SPDX-License-Identifier: MIT
3
4# NixOS VM test exercising zsmtp against third-party implementations:
5# - zsmtp client -> Postfix (25/465) and Exim (2625/2626): plaintext,
6# STARTTLS, and implicit TLS against each, verified by checking local
7# delivery to alice's mailbox
8# - swaks -> zsmtp server: plaintext and STARTTLS, verified by checking
9# the received message in the server's journal
10
11{
12 testers,
13 callPackage,
14 runCommand,
15 openssl,
16}:
17let
18 zsmtp = callPackage ./package.nix { };
19
20 snakeoil =
21 runCommand "zsmtp-test-cert"
22 {
23 nativeBuildInputs = [ openssl ];
24 }
25 ''
26 mkdir -p $out
27 openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
28 -keyout $out/key.pem -out $out/cert.pem -days 36500 -nodes \
29 -subj "/CN=localhost" \
30 -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
31 '';
32in
33testers.runNixOSTest {
34 name = "zsmtp-interop";
35
36 nodes.machine =
37 { pkgs, ... }:
38 {
39 environment.systemPackages = [
40 zsmtp
41 pkgs.swaks
42 ];
43
44 users.users.alice.isNormalUser = true;
45
46 services.postfix = {
47 enable = true;
48 # Implicit-TLS smtpd on port 465; allow loopback without SASL.
49 enableSubmissions = true;
50 submissionsOptions = {
51 smtpd_client_restrictions = "permit_mynetworks,reject";
52 };
53 settings.main = {
54 mydestination = [
55 "localhost"
56 "$myhostname"
57 ];
58 smtpd_tls_security_level = "may";
59 smtpd_tls_chain_files = [
60 "${snakeoil}/key.pem"
61 "${snakeoil}/cert.pem"
62 ];
63 };
64 };
65
66 # Exim: plaintext + STARTTLS on 2625, implicit TLS on 2626, delivering
67 # to /var/spool/exim-mail/<user>.
68 services.exim = {
69 enable = true;
70 config = ''
71 primary_hostname = machine.test
72 qualify_domain = localhost
73 local_interfaces = 127.0.0.1
74 daemon_smtp_ports = 2625 : 2626
75 tls_on_connect_ports = 2626
76 tls_advertise_hosts = *
77 tls_certificate = ${snakeoil}/cert.pem
78 tls_privatekey = ${snakeoil}/key.pem
79 acl_smtp_rcpt = acl_rcpt
80
81 begin acl
82
83 acl_rcpt:
84 accept
85
86 begin routers
87
88 local_users:
89 driver = accept
90 local_parts = alice : bob
91 transport = local_delivery
92
93 begin transports
94
95 local_delivery:
96 driver = appendfile
97 file = /var/spool/exim-mail/$local_part_data
98 user = exim
99 delivery_date_add
100 envelope_to_add
101 return_path_add
102 '';
103 };
104
105 systemd.tmpfiles.rules = [
106 "d /var/spool/exim-mail 0755 exim exim -"
107 ];
108
109 systemd.services.zsmtp-server = {
110 description = "zsmtp debug server (plaintext)";
111 wantedBy = [ "multi-user.target" ];
112 serviceConfig = {
113 ExecStart = "${zsmtp}/bin/zsmtp serve 2525";
114 DynamicUser = true;
115 };
116 };
117
118 systemd.services.zsmtp-server-tls = {
119 description = "zsmtp debug server (STARTTLS)";
120 wantedBy = [ "multi-user.target" ];
121 serviceConfig = {
122 ExecStart = "${zsmtp}/bin/zsmtp serve --tls-cert ${snakeoil}/cert.pem --tls-key ${snakeoil}/key.pem 2526";
123 DynamicUser = true;
124 };
125 };
126 };
127
128 testScript = ''
129 machine.wait_for_unit("postfix.service")
130 machine.wait_for_open_port(25)
131 machine.wait_for_open_port(465)
132 machine.wait_for_unit("exim.service")
133 machine.wait_for_open_port(2625)
134 machine.wait_for_open_port(2626)
135 machine.wait_for_unit("zsmtp-server.service")
136 machine.wait_for_unit("zsmtp-server-tls.service")
137 machine.wait_for_open_port(2525)
138 machine.wait_for_open_port(2526)
139
140
141 def deliver(flags, port, needle, mailbox):
142 machine.succeed(
143 f"printf 'Subject: interop\\r\\n\\r\\n{needle}\\r\\n'"
144 f" | zsmtp send {flags} 127.0.0.1 {port}"
145 " bob@example.com alice@localhost"
146 )
147 machine.wait_until_succeeds(f"grep -r '{needle}' {mailbox}", timeout=60)
148
149
150 servers = {
151 "postfix": (25, 465, "/var/spool/mail/alice/"),
152 "exim": (2625, 2626, "/var/spool/exim-mail/alice"),
153 }
154
155 for name, (port, tls_port, mailbox) in servers.items():
156 with subtest(f"zsmtp client to {name}, plaintext"):
157 deliver("", port, f"zsmtp to {name} plain", mailbox)
158
159 with subtest(f"zsmtp client to {name}, STARTTLS"):
160 deliver(
161 "--starttls --insecure", port, f"zsmtp to {name} starttls", mailbox
162 )
163
164 with subtest(f"zsmtp client to {name}, implicit TLS"):
165 deliver("--tls --insecure", tls_port, f"zsmtp to {name} smtps", mailbox)
166
167 with subtest("swaks to zsmtp server, plaintext"):
168 machine.succeed(
169 "swaks --server 127.0.0.1:2525 --from bob@example.com"
170 " --to alice@example.net --header 'Subject: swaks plain'"
171 " --body 'swaks to zsmtp plain'"
172 )
173 machine.wait_until_succeeds(
174 "journalctl -u zsmtp-server | grep 'swaks to zsmtp plain'", timeout=60
175 )
176
177 with subtest("swaks to zsmtp server, STARTTLS"):
178 machine.succeed(
179 "swaks --tls --server 127.0.0.1:2526 --from bob@example.com"
180 " --to alice@example.net --header 'Subject: swaks starttls'"
181 " --body 'swaks to zsmtp starttls'"
182 )
183 machine.wait_until_succeeds(
184 "journalctl -u zsmtp-server-tls | grep 'swaks to zsmtp starttls'", timeout=60
185 )
186 '';
187}