Skip to content

Instantly share code, notes, and snippets.

@mh-
Last active April 9, 2023 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mh-/b49929b5819112b5687640712a071dfa to your computer and use it in GitHub Desktop.
Save mh-/b49929b5819112b5687640712a071dfa to your computer and use it in GitHub Desktop.
Lightweight SMTP Relay on Raspberry Pi Zero W

Lightweight SMTP Relay on Raspberry Pi Zero W

This can provide a simple local SMTP relay.

A typical use case would be somewhat older devices (like security cameras) that cannot handle TLS while sending e-mails, but your very friendly e-mail provider now insists on that.

The Raspberry Pi Zero W typically consumes about 100..150mA at the USB power connection (less than 0.8W) and can e.g. be powered by the USB port of a router, like the AVM Fritz!Box. (Of course, if you were willing to run a custom firmware on that router, you could directly run the SMTP relay there...)

Initial setup of the Raspberry Pi Zero W:

Use the Raspberry Pi Imager from https://www.raspberrypi.com/software/ to copy Raspberry Pi OS Lite (32 bit) to an SD card. After selecting the version, be sure to click on the Settings icon, activate SSH and set a user name and password. Also set your Wifi SSID and password there. This will enable the Pi to connect to your Wifi network some time after the first boot, and then be available for a connection.

Use ssh pi@raspberrypi.local, or in my case ssh pi@smtprelay.local to connect to your Pi.

On your router, you might want to configure a fixed IP address for your new Pi.

Install postfix:

sudo apt update
sudo apt upgrade

Now follow this is a nice tutorial how to install postfix: https://strobelstefan.de/2020/12/01/e-mail-versand-fuer-den-raspberry-pi-konfigurieren/

Don't forget to do sudo systemctl restart postfix, as mentioned in the tutorial, after any configuration changes.

For reference, this is my /etc/postfix/main.cf config file for postfix:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Raspbian)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2


# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

# The next parameter is important, if e.g. your cameras have a problem with TLS, but still always try that. 
# 'may' is not the right setting in that case. ###############
smtpd_tls_security_level=none

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache


smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
# This is the Raspberry Pi's hostname, which is configured during postfix installation: ###############
myhostname = smtprelay.fritz.box
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, smtprelay, localhost.localdomain, , localhost

# Configure the external mail server here: ###############  
relayhost = smtp.mailserver.de:587
smtp_sasl_auth_enable = yes
sender_canonical_maps = hash:/etc/postfix/sender_canonical
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_enforce_tls = no
smtp_tls_enforce_peername = no

# Configure your local network here: ###############  
mynetworks = 192.168.1.0/24 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 [fe80::]/10 [2000::]/3 [fc00]::/7 
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

Debugging:

To check what postfix was doing, you can check the log: tail /var/log/mail.log

Also, you can connect to your new SMTP server from another machine via:

% telnet smtprelay.local 25
Trying 2003:ef:2714:ed00:9a2e:3350:d7b4:fcd6...
Connected to smtprelay.local.
Escape character is '^]'.
220 smtprelay.fritz.box ESMTP Postfix (Raspbian)
quit
221 2.0.0 Bye
Connection closed by foreign host.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment