Minimalistic Mail Setup for Linux and PHP with ssmtp

With almost every self-set up server you want to have the possibility to send mails. With this setup I describe how you can do this with the least effort.

Maybe you want to be informed about SSH logins or error states. Or you have a web application running with PHP and want to be able to send mails with the PHP mail function. Then this really minimalistic setup based on ssmtp is just right for you. Advantage: You don’t need a complete Set up mail server.

In comparison, here are the packages that are installed with the Mailutils alone, even if you only want to forward mails to a mail server you already use:

guile-2.0-libs libgc1c2 libgsasl7 libkyotocabinet16v5 \
libltdl7 libmailutils4 libntlm0 libpython2.7 libpython2.7-minimal \
libpython2.7-stdlib libunistring0 mailutils-common postfix

Wenn Du noch kein Python auf dem Rechner hast, wir es vermulich noch mehr.

Installation

If you don’t have Python on your computer yet, we’ll probably have more.

Installation

The package ssmtp can do three things: It can send mails via SMTP to your existing mail server, it provides a replacement for the program sendmail and you can define for different Linux users on the system where mails should be sent when they use sendmail, i.e. user specific.

This will install the package on Arch, Manjaro or Ubuntu:

pacman -S ssmpt

or

apt install ssmpt

Configuration

The entire configuration of mail server (for sending via SMTP), user logins, password, mail address and so on is specified in the file /etc/ssmtp/ssmtp.conf (case insensitive):

root=empfaenger@domain.de
rewritedomain=domain.de
hostname=www.domain.de
authuser=emailuser@provider.net
authpass=mysecretpassword
mailhub=smtp.provider.net

The lines have the following meaning:

  • root - The recipient of all system mails that otherwise remain local.
  • rewritedomain - Username + Rewritedomain = Sender
  • hostname - The hostname of our server incl. domain (fqdn)
  • authuser - login name at your mail provider, mostly your email address
  • authpass - Your password at the mailprovider
  • mailhub - The SMTP mail server of your provider

That’s it, now you can send a test mail from Linux. Make a text file test.txt that looks like this (blank line after the subject line is important):

Subject: Terminal Email Send

Dies ist ein test-Text Test!

And now you can send it as Mail:

sendmail empfaenger@domain.de < test.txt

If you have entered a wrong password or something else goes wrong, you can analyze the situation this way:

tail /var/log/mail.log

PHP configuration

If you have installed PHP on your Linux server, you can use the above configuration: Find the php.ini file you are using, for me it is /etc/php/7.3/apache2/php.ini and enter the following:

sendmail_path = /usr/sbin/sendmail
sendmail_from = username@domain.de

You can find out the upper value by typing which sendmail, the lower value is the default sender if nothing else is specified in your PHP program. The latter should have the back domain part as your rewritedomain is. Example: If you send from your server, it is called www.domain.de or chat.domain.de, where domain.de is your domain, which you have to set as it is called.

Security

The /etc/ssmtp/ssmtp.conf file must be secured so that only the root user can read and write it and the mail user can read it:

chown root:mail /etc/ssmtp/ssmtp.conf
chmod 640 /etc/ssmtp/ssmtp.conf

It looks that way with ls -l:

-rw-r----- 1 root mail 600 Apr 29 15:39 ssmtp.conf

You can configure a few more things, but that’s all you need for now. No bloat, no thousand dependencies, small and slim and just what you need. Have fun with it.

User with mail dispatch

The ssmtp tool uses the users of the Linux system. Now it can be defined for each Linux user as which user he appears to the outside when sending mails.

Example: Everything that is sent by the user root should go to an admin mail account that you have set up especially for this purpose, i.e. a separate mailbox. The same goes for the www-data user - that is everything that comes from the web server or from your web applications, e.g. from PHP.

For this we adapt the file /etc/ssmtp/revaliases:

# sSMTP aliases
# 
# Format: local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# Where [:port] is an optional port number that defaults to 25.

root:admin@domain.de:mailserver.de
www-data:www@domain.de:mailserver.de

Usernames in the mail

Now the sending works, but the recipient of the mails should not have root or www-data as sender, but perhaps a more beautiful readable name, like Administrator.

We configure this by adjusting the readable names in the file /etc/passwd in the fifth column, separated by colons, for example we change:

root:x:0:0:root:/root:/bin/bash

to

root:x:0:0:Administrator:/root:/bin/bash

These readable names are then used automatically. Also for the www-data user, if used. For example, you can choose the name of the organization or the name of a specific web application you have set up, such as Forum.

Conclusion

You can configure a few more things, but that’s all you need for now. No bloat, no thousand dependencies, small and slim and just what you need. Have fun with it.