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.
aptitude search ~c will show what is leftover (I should probably have started with that).
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).
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.confshould look like this:
firstname.lastname@example.org 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 AuthUseremail@example.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: firstname.lastname@example.org email@example.com # /etc/ssmtp/revaliases: root:firstname.lastname@example.org:smtp.somemailbox.org:465 username:email@example.com:smtp.somemailbox.org:465
disallow users to set their own from address in /etc/ssmtp/ssmtp.conf:
Send a test mail:
# ssmtp firstname.lastname@example.org
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: email@example.com From: firstname.lastname@example.org 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.