Cover Image

Minimalistic Mail Setup for Linux and PHP with ssmtp

April, 29. 2019f 2019 - Reading time: 5 minutes

With almost every self-set up server you want to have the possibility to send mails. With this setup 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 to set up a whole 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

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
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.com
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

This is a test text test!

And now you can send it as Main:

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 then:

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

About

The World in the Eyes of Mr. Doering.

Experiences, Mentions, Inspirations, Emotions and Experiences

Der deutsche Blog ist hier.