FacebookTwitter

Make PHP Mail() Work in AWS EC2 – Ubuntu

AWS IP addresses can be problematic for setting an email server in an EC2 instance. Here is an alternative method to get Mail() working.

By on Aug 14, 2014 in Geek | 0 comments

Share On GoogleShare On FacebookShare On Twitter

AWS IP addresses can be problematic for setting an email server in an EC2 instance…even with right setup email sent from the IP address can be marked as spam.

So what to do with setting up SMTP for your web applications? One alternative is to use AWS SES service. I decided not to use SES, however, because 1) each email address or domain needs to be approved and 2) there is cost associated with number of email sent out. These aren’t too bad of the restrictions but I wanted something that JUST work without configuring every time I build a website.

I went with MSMTP to use my gmail account to send email message by default. I can also overwrite the smtp setting per linux account. So if I were to host sites that are not mine, they can use their own smtp setup.

How to install MSMTP in Ubuntu

In terminal, type

sudo apt-get install msmtp

How to setup SMTP Server Using MSMTP in Ubuntu

I followed the instructions from the two articles I listed at the bottom of this page. I am rewriting the instructions based on my experience for setting up with Gmail here with strong emphasis on testing the setting in each step.  I skipped some and ended up spending a chunk of time trying to make it work.

1. Setup MSMTP setting at user level FIRST!

Create a file “.msmtprc” in the root of the user folder. (I use nano via terminal)

nano ~/.msmtprc

and type the following in the file with your account credential

account gmail
tls on
tls_certcheck off
auth on
host smtp.gmail.com
port 587
user user1@gmail.com
from user1@gmail.com
password yourgmailPassw0rd

Note – one of the articles I mentioned did not include “tls_certcheck off” in the file. In my case it didn’t work without that line. So make sure to include that line.

change the permission level of the .msmtprc to 600

chmod 600 ~/.msmtprc

2. Test the setting with MSMTP…because if it doesn’t work with MSMTP command it  won’t work with PHP.

Create a test email file. It’s easier to make a file than type an email on command line.

nano test_email

in the email file type the following with your choice of from and to email addresses

From: Fuyuko <sourceemailaddress@gmail.com>
To: Bob <destinationemailaddress@example.com>
Subject: Test Email
This is a test email message

Try sending this using MSMTP

cat test_email | msmtp -a gmail destinationemailaddress@example.com

3. Bring the MSMTP setting to Server Level

Copy the .msmtprc file that you created to “/etc/” folder with a new name “msmtprc” without “.”

sudo cp ~/.msmtprc /etc/msmtprc

You need to do this with “sudo” because you won’t be able to access “etc” folder otherwise

Then, change the ownership and permission level of the msmtprc in “etc” folder. so that only the web server can access the file.

sudo chown www-data /etc/msmtprc
sudo chmod 600 /etc/msmtprc

4. Configure php.ini to use this setting for mail()

sudo nano /etc/php5/apache2/php.ini

Note – this is the path to php.ini under ubuntu 14.04 environment. The path could be different with your server

Look for a line with “sendmail_path” and modify:

sendmail_path = "/usr/bin/msmtp -C /etc/msmtprc -a gmail -t"

Note – again the path to msmtp may be different in your server. You can find out by typing “whereis msmtp” in your terminal.

Save the file and restart the web server:

sudo service apache2 restart

5. Test PHP mail() function

Create a test php file. test_mail.php

<?php mail("test@example.com", "test email", "test email message"); ?>

Open this file and see if the email is delivered

6. Debugging mail()

Check the server’s error log file and see if there is any MSMTP related error.  The default error log location under Ubuntu 14.04 environment is “/var/log/apache2/error.log”

That came handy for me. In my case I misspelled the location of MSMTP in php.ini (instead of  “usr” I typed “user”).

Resource: How To Use Gmail or Yahoo with PHP mail() Function
Resource: How to use MSMTP with Gmail, Yahoo and PHP Mail

Leave a Reply