Sending email from PHP in Vagrant via sSMTP and Office365

I have a PHP app that needs to send email, and it recently moved from a real server to a Vagrant box (don’t ask!) which was not configured to send email out to the real word.

After trying a few different options the one that ended up working for me was to use sSMTP to send the email via our Office365 system. This is how I got it to work.

sSMTP and Office365

Firstly I had to get sSMTP installed and talking nicely to our Office365 SMTP server. Step one – install sSMTP.

sudo apt-get install ssmtp

I got messages about php.ini being modified … I chose to keep the local version, since it had extra stuff added that I needed. If you haven’t touched your php.ini file, just install the package maintainer’s version.

Then I edited /etc/ssmtp/ssmtp.conf thus:

and /etc/ssmtp/revaliases thus:

Note that you’ll need to get your own Office 365 SMTP config settings which might be different to mine. To find them, log in to Outlook Web Access (ie Outlook in the browser) and click the settings cog at the top right, then Mail. Then in the menu that appears on the left, click Accounts -> POP and IMAP, and take note of the settings in the SMTP section.

Note also that this contains your real Outlook password. I’ve decided that’s OK for a Vagrant box on my PC that nobody else can get to … you need to determine your own security arrangements.

Note also that you need to use your username in some places and your real email address in other places. And it needs to be your address – the one for the username and password you’re using – otherwise Office365 will think you’re pretending to be someone you’re not, and it will refuse to deliver the mail.

And I haven’t shown all the comments in the files, but of course there’s no harm leaving them in if you want to.

Check that sSMTP is working

Before you move on to PHP, it’s worth checking that sSMTP is working. So enter this at the command line to send a test email:

echo -e "\nSubject: From sSMTP\n\nThis is a test email." | ssmtp -d

Replace with your real address (or Office365 won’t accept the email), and replace with the address you want to send the email to. If it’s all set up correctly, there should be a new email in the recipient’s inbox.

Getting PHP to use sSMTP instead of sendmail

This requires editing your php.ini file – and if you have several, you need to edit the one that PHP is actually using. I temporarily added a phpinfo() page to my PHP app to see which php.ini file was being used, and for me it was /etc/php/7.0/fpm/php.ini.

So find the right php.ini file and change this line:

;sendmail_path =

…to this:

sendmail_path = /usr/sbin/ssmtp -t

…and try sending an email from your PHP code.

Working – but slow

Email is now working, but it goes very slowly. That might be because the hostname needs to be a fully qualified domain name for mail to send smoothly. So the last thing I did was to edit /etc/hosts to change the first line and changed the hostname in /etc/hostname from homestead to

Though of course Vagrant then changed the hostname back as soon as I halted and brought it back up again, so I then went to  C:\Vagrant\[myVagrantBox]\scripts\homestead.rb on my host machine and changed config.vm.hostname = settings["hostname"] ||= "homestead" to config.vm.hostname = "".

But this didn’t help much. So email is still slow.      😦     But it works, so I’m going to leave it for now.

Still to do…

This is now working fine for me, but if I ever have to re-build my Vagrant box, I’ll have to do this setup all over again. Which is not the way Vagrant is meant to work.

So my next step will be to include this in a Vagrant script … once I re-check the syntax on moving files and work out what to do about the password.

Sending email from PHP in Vagrant via sSMTP and Office365

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s