Generate an RFC-conforming message¶
Mailclients and applications must produce valid headers and MIME. Postfix does not automatically repair an invalid author address.
Setup¶
Prepare your values and Debian packages. CLI tests use only your local test files and configuration.
Use a MIME library in your own applications. This single block creates a synthetic message using your addresses. To test a real application, save its original .eml file at the same test path instead.
set -euo pipefail
. /root/mailserver.env
install -d -m 0700 /root/mail-tests
python3 - <<'PY'
from email.message import EmailMessage
from email.policy import SMTP
from email.utils import formatdate, make_msgid
from pathlib import Path
import os
message = EmailMessage()
message["From"] = os.environ["MAILBOX"]
message["To"] = os.environ["MAILBOX"]
message["Date"] = formatdate(localtime=False, usegmt=True)
message["Message-ID"] = make_msgid(domain=os.environ["DOMAIN"])
message["Subject"] = "Local mail server functional test"
message.set_content("This is a synthetic local test message.", charset="utf-8")
Path("/root/mail-tests/sample.eml").write_bytes(message.as_bytes(policy=SMTP))
PY
Test¶
set -euo pipefail
. /root/mailserver.env
python3 - <<'PY'
from email.parser import BytesParser
from email.policy import default
from email.utils import getaddresses, parsedate_to_datetime
from pathlib import Path
import os, re
raw = Path("/root/mail-tests/sample.eml").read_bytes()
message = BytesParser(policy=default).parsebytes(raw)
assert b"\r\n\r\n" in raw
assert b"\n" not in raw.replace(b"\r\n", b"")
assert not message.defects, message.defects
assert len(message.get_all("From", [])) == 1
assert getaddresses(message.get_all("From"))[0][1] == os.environ["MAILBOX"]
print("PASS: CRLF, header/body separation and a single From header are correct.")
PY
This is a basic syntax and generation test, not a complete validator for every RFC edge case. Keep the original file and fix the generating application if errors occur. The synthetic subject, body and some diagnostic messages are German so that both language editions retain the exact validated commands.