February 5th 2016

linux, server, email

Getting PHP email to work with ssmtp and picky email providers

My server is running debian stable.
Recently I had been messing around with different mailing setups, which resulted in total breakage of email sending, as often required by a CMS, usually via php and sendmail.

Remove unneeded stuff

I was playing with dovecot and exim4 before I realised that what I really want is sSMTP.

I purged all packages that contain 'dovecot' and 'exim4' in their names, and always performed an "apt-get --purge autoremove" afterwards. Then I went through all leftover configuration & modifications in /etc and removed them manually.
There was a lot of it, even after purging the packages.

But it seems that even after that some residual configuration files are interfering.
This command: aptitude search ~c will show what is leftover (I should probably have started with that).
This command: aptitude -s purge ~c will simulate the removal, and this: aptitude purge ~c will finally do it.

If you don't want to use aptitude:
dpkg -l | grep ^rc to see what residual config files are left over and
for i in $(dpkg -l | grep ^rc | cut -d” ” -f3); do dpkg –purge $i; done
to commit.



With a hopefully clean system, install ssmtp and mailutils: apt-get install ssmtp mailutils.

Configuration for Gmail:
  • activate 'allow less secure apps' from your gmail profile
  • /etc/ssmtp/ssmtp.conf should look like this:
    rewriteDomain=nn.nn.nn.nn (my IP)
Configuration for picky email provider:

Fix permissions on /etc/ssmtp/ssmtp.conf etc. as outlined here (also for /etc/ssmtp/revaliases, see below).

But that only enabled me to send mail with the same "From" address as is my account's. It's a specialty of picky email provider, i guess. to make it work with my website's email form, i additionally had to:

  • set up both rewriting in /etc/ssmtp/ssmtp.conf and /etc/ssmtp/revaliases both for root and normal user:

    # /etc/ssmtp/ssmtp.conf:
    # /etc/ssmtp/revaliases:
  • disallow users to set their own from address in /etc/ssmtp/ssmtp.conf:


Send a test mail:
# ssmtp recipient_email@example.com

You will most likely need to be root for this.

sSMTP will then wait for you to type your message, which needs to be formatted like this:

To: recipient_email@example.com
From: myemailaddress@gmail.com
Subject: test email

hello world!

Note the blank like after the subject, everything after this line is the body of the email. When you’re finished, press Ctrl-D. sSMTP may take a few seconds to send the message before closing.