Home » Web Development » Devops » Sending Mail Using PHP and Amazon SES on Centos/AWS Linux
Sending Mail Using PHP and Amazon SES on Centos/AWS Linux

Sending Mail Using PHP and Amazon SES on Centos/AWS Linux

If you find yourself using Amazon SES for sending outgoing emails in a PHP web app, getting everything set up is much simpler than it may seem. In my case, this was on an AWS Linux image, but it will work on any Fedora/CentOS AMI.

You can, of course, set up sendmail and use a proper MTA to send email from your web app, but in this case, I had inherited the requirement to set up an environment for code that was already written to use SES for outgoing email and Google Apps for incoming emails, so the actual mechanics weren’t up for debate.

The easiest way to handle this is to set up an SMTP client that can easily relay, such as Msmtp. Start to finish, setting this up took about 5 minutes.

When you sign up for Amazon SES, you’ll need to set up the domains you want to send from, add some DNS entries (which is nice and easy if you’re using Route 53 already for DNS)…

And then verify a few email addresses to Amazon knows you’re not trying to send email from an account you shouldn’t…

So now your SES is all set up, and you just need to set up Msmtp to handle your outgoing email.

While logged into your AWS instance via command line, do the following. Notice that your version number may be different from mine in the example:

# Grab the latest Msmtp package from SF
wget http://sourceforge.net/projects/msmtp/files/latest/download?source=files
tar -xjvf msmtp-1.4.30.tar.bz2
cd msmtp-1.4.30 

# Now switch to root and set up Msmtp
su root

# If you don't have gcc, make, openssl 
# and openssl-devel, install them
yum install gcc make openssl openssl-devel

./configure
make
make install
make clean

# and now create the Msmtp config file and log file
# and make sure the log file is writable
touch /usr/local/etc/msmtprc
touch /var/log/msmtp.log
chmod 666 /var/log/msmtp.log

Next we need to find the path for the server’s CA certificate, so we can use this in the Msmtp config file.

find / -name *.crt

This was located at /etc/pki/tls/certs/ca-bundle.crt for me, but might be located somewhere else if you’re not using Amazon Linux or CentOS.

Now we need to set up Msmtp to use your SES login information:

vi /usr/local/etc/msmtprc

You can use the settings below (replacing the placeholder username and password with the one from your own SES account, of course) and paste this into your empty /usr/local/etc/msmtprc file. Also be sure to set a default “from” address in your config. You can override this value in your php scripts, but it’s still good to set a valid default.

account default
host email-smtp.us-east-1.amazonaws.com
port 587
timeout 30
auth on
user YOUR_SES_USERNAME
password YOUR_SES_PASSWORD
auto_from off
from VALID_FROM_ADDRESS
maildomain yourdomain.com
tls on
tls_starttls on
tls_trust_file /etc/pki/tls/certs/ca-bundle.crt
logfile /var/log/msmtp.log

Now we just need to change your php.ini file to point the sendmail_path to Msmtp executable instead of the default sendmail:

vi /etc/php.ini

Be sure to leave the two flags -t and -i after the sendmail_path value. My updated php.ini sendmail_path looks like:

sendmail_path: /usr/local/bin/msmtp -t -i

Then restart your web server – apache for me.

service httpd restart 

Now let’s try a quick mail script to make sure this is working:

$to      = 'YOUR_EMAIL';
$subject = 'Test subject from amazon aws ses';
$message = 'this is just a test!';
$headers = 'From: VERIFIED_SES_EMAIL_ADDRESS' . "\r\n" .
    'Reply-To: VERIFIED_SES_EMAIL_ADDRESS' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Hopefully, you should get your test email pretty soon after. (It was immediate for me.) And just to be thorough, let’s check the msmtp log to make sure everything looks like it went smoothly:

tail -f /var/log/msmtp.log

You should see an entry in your msmtp.log that shows a 200 OK status for the email you just sent through SES. You should also now see 1 message sent in your SES console that shows you how many messages have been sent through your system.

And that’s it!

Advertisement

Graphic River

Advertisement

Site5

flattr this!

About snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist, and the CTO at Mass Mosaic in New York City. Tweet at me @snipeyhead or read more...
  • Thomas Kekeisen

    Thank you, that helped me a lot. Here is the config file to use it with elastic beanstalk:

    container_commands:

    01_copy_msmtp:

    command: “cat .ebextensions/msmtp-1.4.32.tar.bz2 > /tmp/msmtp-1.4.32.tar.bz2 && chmod 644 /tmp/msmtp-1.4.32.tar.bz2″

    02_unpack_msmtp:

    cwd: /tmp

    command: “tar -xjvf msmtp-1.4.32.tar.bz2″

    03_cd_to_msmtp:

    command: “cd /tmp/msmtp-1.4.32″

    04_check_for_missing_packages:

    command: “yum install gcc make openssl openssl-devel”

    05_configure:

    cwd: /tmp/msmtp-1.4.32

    command: “./configure”

    06_make:

    cwd: /tmp/msmtp-1.4.32

    command: “make”

    07_make_install:

    cwd: /tmp/msmtp-1.4.32

    command: “make install”

    08_make_clean:

    cwd: /tmp/msmtp-1.4.32

    command: “make clean”

    09_create_logs_1:

    command: “touch /usr/local/etc/msmtprc”

    10_create_logs_2:

    command: “touch /var/log/msmtp.log”

    11_fix_log_rights:

    command: “chmod 666 /var/log/msmtp.log”

    • http://www.snipe.net snipe

      Awesome, thanks for the additional info!