Skip to content

Instantly share code, notes, and snippets.

@Zamana
Last active February 24, 2024 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zamana/f5c8f4b67290e2a2c8b8339e7b169d6a to your computer and use it in GitHub Desktop.
Save Zamana/f5c8f4b67290e2a2c8b8339e7b169d6a to your computer and use it in GitHub Desktop.
Mailrelay in FreeBSD or FreeNAS/TrueNAS jail

Installing mailrelay on FreeBSD or TrueNAS jail

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.

1. Install the Go language package:

pkg install go

2. Get mailrelay sources

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.

2.1 Using Git

Install the git package (the "lite" version is enough):

pkg install git-lite

Then clone the repository and enter in the folder created:

git clone https://github.com/wiggin77/mailrelay.git
cd mailrelay

2.2 Using Zip

Go to the mailrelay Github page, click on Code, then right-click on "Download ZIP" and then choose "Copy link address":

image

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-master

3. Build the mailrelay

Inside 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-amd64

If everything ran fine, the mailrelay will be at ./build/freebsd_amd64/mailrelay-freebsd-amd64

4. Installation and configuration

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/mailrelay

Create 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/mailrelay

Create 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/mailrelay

Create 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": "sbrubles@gmail.com",
    "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
}

5. Testing

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 -sender=sbrubles@gmail.com -rcpt=sbrubles@gmail.com

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: sbrubles@gmail.com / to: [{sbrubles gmail.com [] [] false false <nil>  false}]
INFO[0000] Headers are:map[From:[sbrubles@gmail.com] Subject:[Test message]]
INFO[0000] starting email send -- from:sbrubles@gmail.com, starttls:false
INFO[0002] email sent with no errors.

If not, check and recheck the previous steps and the configuration files.

6. Running as a service (daemon)

Now, enable the service to run at boot with this command:

sysrc mailrelay_enable=YES

And now you are ready to control the service with:

service mailrelay start | stop | status

In 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 mailrelay

or

netstat -an | grep -iw listen

Congratulations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment