mailrelay is a great software written by Doug Lauder.
It's purpose is to be a simple SMTP server that allows you to send e-mails from the computers in your LAN through an SMTP relay.
It's in the middle of the way between a full blown SMTP server like Postfix, and a simple, local-only, ssmtp command, which is great for some purposes.
Here are the instructions in order to get mailrelay configured and automatically running on a FreeBSD system or in a TrueNAS jail.
pkg install go
Here you have the option to use Git or download the sources directly. It doesn't matter what you choose, the final product will be the same. The advantage of the Git way is that is easier to keep mailrelay updated.
Follow 2.1 or 2.2 as you wish.
Install the git package (the "lite" version is enough):
pkg install git-liteThen clone the repository and enter in the folder created:
git clone https://github.com/wiggin77/mailrelay.git
cd mailrelayGo to the mailrelay Github page, click on Code, then right-click on "Download ZIP" and then choose "Copy link address":
Then execute the fetch command to download the zip file, by pasting the copied address in the command line, and then run the unzip command to extract it and enter in the folder created:
fetch https://github.com/wiggin77/mailrelay/archive/refs/heads/master.zip
unzip master.zip
cd mailrelay-masterInside the folder with the source-code, excecute the following one line command (copy and paste if you prefer):
env GOOS=freebsd GOARCH=amd64 go build -o ./build/freebsd_amd64/mailrelay-freebsd-amd64If everything ran fine, the mailrelay will be at ./build/freebsd_amd64/mailrelay-freebsd-amd64
This instructions assumes that mailrelay will be run by the root user.
Copy the binary to the /usr/local/bin folder:
cp ./build/freebsd_amd64/mailrelay-freebsd-amd64 /usr/local/bin/mailrelayCreate the folders for the init script (if it doesn't already exists) and for the mailrelay.json config file:
mkdir /usr/local/etc/rc.d
mkdir /usr/local/etc/mailrelayCreate a file named mailrelay in the folder /usr/local/etc/rc.d, with the following content:
#!/bin/sh
#
# Author: C. R. Zamana (czamana at gmail dot com)
#
# PROVIDE: mailrelay
# REQUIRE: network
# KEYWORD: shutdown
. /etc/rc.subr
name="mailrelay"
rcvar="${name}_enable"
load_rc_config ${name}
: ${mailrelay_enable:="NO"}
: ${mailrelay_bin:="/usr/local/bin/mailrelay"}
: ${mailrelay_config:="/usr/local/etc/mailrelay/mailrelay.json"}
pidfile="/var/run/mailrelay.pid"
PATH=$PATH:/usr/local/bin
command="/usr/sbin/daemon"
command_args="-P ${pidfile} ${mailrelay_bin} -config ${mailrelay_config} > /dev/null"
run_rc_command "$1"Give it the execute permissions:
chmod +x /usr/local/etc/rc.d/mailrelayCreate the configuration file mailrelay.json in /usr/local/etc/mailrelay according with your SMTP relay server. Here is just an example using Gmail as the relay server:
{
"smtp_server": "smtp.gmail.com",
"smtp_port": 465,
"smtp_starttls": false,
"smtp_username": "[email protected]",
"smtp_password": "passwordforsbrubles",
"smtp_max_email_size": 10485760,
"smtp_login_auth_type": false,
"local_listen_ip": "0.0.0.0",
"local_listen_port": 25,
"allowed_hosts": ["*"],
"timeout_secs": 30
}In order to test your installation, execute the follwing command, changing the e-mail address accordingly:
mailrelay -config=/usr/local/etc/mailrelay/mailrelay.json -test [email protected] [email protected]If everything was fine and ran fine, you'll see something like this:
INFO[0000] processing worker started (#1)
INFO[0000] Starting: 0.0.0.0:25
INFO[0000] processing worker started (#2)
INFO[0000] Listening on TCP 0.0.0.0:25
INFO[0000] processing worker started (#3)
INFO[0000] main log configured to stdout
INFO[0000] Handle client [::1], id: 1
INFO[0000] Mail from: [email protected] / to: [{sbrubles gmail.com [] [] false false <nil> false}]
INFO[0000] Headers are:map[From:[[email protected]] Subject:[Test message]]
INFO[0000] starting email send -- from:[email protected], starttls:false
INFO[0002] email sent with no errors.
If not, check and recheck the previous steps and the configuration files.
Now, enable the service to run at boot with this command:
sysrc mailrelay_enable=YESAnd now you are ready to control the service with:
service mailrelay start | stop | statusIn order to check that the service is really running and listening at the configured port you can use:
service mailrelay status
or
ps -aux | grep mailrelayor
netstat -an | grep -iw listenCongratulations!
