dt.iki.fi

Getting PHP email to work with ssmtp and picky email providers

My server is running debian stable (written when jessie was stable, all this still applies to stretch).
Recently I had been messing around with different mailing setups, which resulted in total breakage of email sending, as often used by a PHP contact form on a website.

Remove unneeded stuff

I was playing with dovecot and exim4 before I realised that they are overkill for my needs and what I really want is sSMTP, an "extremely simple MTA to get mail off the system to a mail hub".

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 (source).

Installation

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:
    root=someone@gmail.com
    mailhub=smtp.gmail.com:587
    #rewriteDomain=nn.nn.nn.nn (my IP) # should not be required
    #hostname=nn.nn.nn.nn # should not be required
    FromLineOverride=YES
    UseTLS=YES
    UseSTARTTLS=YES
    AuthUser=someone@gmail.com # same as above
    AuthPass=verysecret
    AuthMethod=LOGIN

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:
    root=someone@somemailbox.org
    normaluser=someone@somemailbox.org
    # /etc/ssmtp/revaliases:
    root:someone@somemailbox.org:smtp.somemailbox.org:465
    username:someone@somemailbox.org:smtp.somemailbox.org:465
  • disallow users to set their own from address in /etc/ssmtp/ssmtp.conf:

    FromLineOverride=NO

Source

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.

Source