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:

[bash]# Grab the latest Msmtp package from SF
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

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[/bash]

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

[bash]find / -name *.crt[/bash]

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:

[bash]vi /usr/local/etc/msmtprc[/bash]

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.

[plain]account default
port 587
timeout 30
auth on
auto_from off
tls on
tls_starttls on
tls_trust_file /etc/pki/tls/certs/ca-bundle.crt
logfile /var/log/msmtp.log[/plain]

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

[bash]vi /etc/php.ini[/bash]

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

[plain]sendmail_path: /usr/local/bin/msmtp -t -i[/plain]

Then restart your web server – apache for me.

[bash]service httpd restart [/bash]

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:

[bash]tail -f /var/log/msmtp.log[/bash]

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!

  • Thomas Kekeisen

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



    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”


    cwd: /tmp

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


    command: “cd /tmp/msmtp-1.4.32”


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


    cwd: /tmp/msmtp-1.4.32

    command: “./configure”


    cwd: /tmp/msmtp-1.4.32

    command: “make”


    cwd: /tmp/msmtp-1.4.32

    command: “make install”


    cwd: /tmp/msmtp-1.4.32

    command: “make clean”


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


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


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

    • Awesome, thanks for the additional info!