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:

root=your.email.address@your-domain.com
mailhub=smtp.office365.com:587
rewriteDomain=your-domain.com
hostname=your-domain.com
FromLineOverride=YES
AuthUser=username@your-domain.com
AuthPass=your_real_outlook_password
UseSTARTTLS=YES
UseTLS-YES
AuthMethod=LOGIN

and /etc/ssmtp/revaliases thus:

root:username@your-domain.com:smtp.office365.com:587
mainuser:username@your-domain.com:smtp.office365.com:587

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’ve removed all the comments for this post – 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 "From:your.email@your-domain.com\nSubject: From sSMTP\n\nThis is a test email." | ssmtp -d someone@somewhere.com

Replace your.email@your-domain.com with your real address (or Office365 won’t accept the email), and replace someone@somewhere.com 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/php5/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 myproject.app.

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 = "myproject.app".

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.

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s