Configure mail sending

From Organic Design
Procedure.svg Configure mail sending
Organic Design procedure

Mail sending is complicated, it requires that the sending server have valid reverse DNS, DKIM signature, SPF and DMARC records. If you just need basic sending capability, then you don't need to configure a full mail server, it's much simpler and lighter weight to use an external mail sending service, so we can simply connect to their SMTP server to send mail.

To make this setup as transparent and light-weight as possible, we use the MSMTP simple sendmail alternative. That way all applications on the server can send email using the local sendmail utility as expected, but the actual job of sending is delegated to the remote SMTP server.

apt install msmtp


Make sure that Exim4 is not installed (use dpkg -l to see what's installed), and that /usr/bin/sendmail points to msmtp, so that default system mail mechanisms will work properly.

dpkg -l | grep exim
apt remove exim4-daemon-light
ln -s /usr/bin/msmtp /usr/sbin/sendmail


Msmtp configuration is /etc/msmtprc, a basic configuration to simply forward every by default to a remote SMTP server is as follows (of course you'll need to change to the appropriate details for the server you're using):

account default
syslog LOG_MAIL
maildomain organicdesign.co.nz
host smtp.organicdesign.co.nz
auto_from on
port 587
tls on
tls_starttls on
auth plain
user YOUR_USER_NAME
password YOUR_UNIX_PASSWORD


There will likely be no program installed for the mail command to work, so if you need this then a very basic Perl script can be installed in /usr/local/bin/mail as follows:

#!/usr/bin/perl
my $input = join( '', <STDIN> );
qx( echo "Subject: $ARGV[1]\nContent-type: text/plain; charset=utf-8\n\n$input" | msmtp $ARGV[2] );

Note: This is extremely basic and only allows the absolute simplest use of the mail command, where the body is sent to STDIN and a subject is supplied with the -s option followed by the recipient email address.


Then test sending a message to yourself using all standard methods that are expected to work:

echo "testing" | mail -s "mail test" foo@bar.baz
echo "Subject: sendmail test" | sendmail -v "foo@bar.baz"
echo '<?php mail("foo@bar.baz","php test","testing");' | php

See also