Sign inSign up

netebakari/simple-mail-relay

By netebakari

•Updated over 1 year ago

Simple mail relay server built with Postfix and OpenDKIM

Image
Networking
Developer tools
0

405

netebakari/simple-mail-relay repository overview

⁠What is this?

A mail relay server. When you send mail to this container, it forwards the mail externally with a DKIM signature, depending on the sender domain. Multiple domains supported. The DKIM selector is fixed to default.

GitHub repository is here⁠. Japanese document is here⁠.

⁠Motivation

  • I want a simple mail relay server and know how DKIM works.
  • I want to keep logs of when, to whom, and what kind of emails were sent, as long as storage capacity allows.
  • I want to output Postfix logs to standard output for easy handling.

⁠Logs

⁠Postfix Logs

Since logs are output to standard output, you can check them with docker logs or docker compose logs.

⁠Mail Logs

Two types of log files are output:

  • CSV file summarizing the mail's timestamp, subject, and recipient in one line.
  • Plain text file containing all information, including the mail header, body, attachment of single mail.

⁠How to Start

⁠1. Create Log Directories

$ mkdir -p logs/list
$ mkdir -p logs/raw
$ chmod 777 logs/list logs/raw
  or
$ chown YOUR-USER-WHOSE-ID-IS-1000 logs/list logs/raw

⁠2. DKIM Configuration

⁠Create DKIM keys
$ mkdir keys

Create DKIM public/private keys using the opendkim-genkey command and put them into the keys/ directory. You can store keys for multiple domains.

The filename should be in the format FQDN + .private, such as example.com.private.

⁠Register keys in your DNS

Register the public key in your DNS TXT record. Since DKIM is verified by the recipient, you can skip this step if you are just testing.

$ dig +short txt default._domainkey.example.com
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0...."

⁠3. Start

$ docker run -d \
  -v `pwd`/keys/:/keys/ \
  -v `pwd`/logs/:/maillogs/ \
  -e SERVERNAME=postfix.example.com \
  -p 25:25 \
  -p 1025:25 \
  netebakari/simple-mail-relay:latest

With this configuration, the container will deliver received mail directly to the internet. I strongly advise you to first build a separate mail receiving container, forward all mail to it, and thoroughly test the setup. Refer to the GitHub repository⁠ for the procedure.

⁠4. Send Test Email

⁠Send a mail

Now you can connect to port TCP/25 or TCP/1025 of localhost using TELNET or something you would like and send emails. If you write an email address of a domain with DKIM configuration completed in From, it will be signed.

$ telnet localhost 1025
HELO localhost
MAIL FROM: [email protected]
RCPT TO: [email protected]
DATA
From: [email protected]
To: [email protected]
Subject: Test
Hello World!
.
QUIT
⁠Check the mail logs

Check the email body in logs/raw/YYYY-MM-DD/. It would be just like:

From [email protected]  Fri Feb 14 20:11:54 2025
Return-Path: <[email protected]>
X-Original-To: logging@localhost
Delivered-To: logging@localhost
DKIM-Filter: OpenDKIM Filter v2.11.0 localhost C23B4466FA
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com;
	s=default; t=1739596314;
	bh=z85OKVJZHnmg3qFlSpLbpPCZ00irfBdrzQUtabiSl3A=;
	h=From:To:Subject:From;
	b=KItSmTGe5svNXMXq02JJo+p8BoJhVbX2tmVatA9I2FfMQpEc6gC9z6irv9XGQkiTe
	 VKCgcl1m8PMcDSskMKP0D7Y0iQkxw57BffLjCSzFL/aiDB7ZhG1XXip3KEXPfIDuq7
	 zp7pwBuXag/tAa1edfPab06lCDGetrPiblUkTIycltW76RltAi0dzDE3KukV/l/Nwt
	 e6wxWcqUDfbngJXE4GcPLk2EpvLuEL+Z41M3OTe6EM4GXgUTsklKDXKzXV8Jkyl/lC
	 oFiZq6f3gCAUNN9GTyI1/fljQzOHCwTNceN/OLYuc8biIs/X8qtrXPIHn22fuXe6o0
	 JDWeLykgVnbew==
Received: from localhost (unknown [172.17.0.1])
	by localhost (Postfix) with SMTP id C23B4466FA
	for <[email protected]>; Fri, 14 Feb 2025 20:11:41 -0900 (AKST)
From: [email protected]
To: [email protected]
Subject: Test

Hello World!

⁠5. Custom Transport File

If you would like forwar emails to a specific server, create a Postfix transport⁠ file.

localhost   local:
*           smtp:[mailcatcher.local]:1025

And mount this file at startup.

$ docker run -d \
  -v `pwd`/keys/:/keys/ \
  -v `pwd`/logs/:/maillogs/ \
  -e SERVERNAME=postfix.example.com \
  -v your_transport_file:/etc/postfix/transport \
  -p 25:25 \
  -p 1025:25 \
  netebakari/simple-mail-relay:latest

Tag summary

Content type

Image

Digest

sha256:ec37ce8b6…

Size

56.7 MB

Last updated

over 1 year ago

docker pull netebakari/simple-mail-relay