Get up and running quickly with Amazon SES on your php website

Jan 28 2011 Published by under Info


Note: if you want to setup SES in a way that scales much better and functions even with non-PHP sites, please read this more recent HowTo: How to configure your Postfix server to relay email through Amazon Simple Email Service (SES)



Here’s how you can start using Amazon’s new SES (Simple Email Service) without having to actually implement it in the php of your website:

  • Extract the files and create a new one named “aws-credentials” with your key data in it; for example:

AWSAccessKeyId=022QF06E7MXBSH9DHM02
AWSSecretKey=kWcrlUX5JEDGM/LtmEENI/aVmYvHNif5zB+d9+ct

  • Verify an email address to use with SES

./ses-verify-email-address.pl -k ./aws-credentials -v someaddress@yourdomain.com

  • Check the email account for the address you’re verifying and click on the provided link.
  • Send a test email:

echo "This is only a test." | ./ses-send-email.pl -k ./aws-credentials -s "test subject for email" -f someaddress@yourdomain.com someaddress@yourdomain.com
(Note – Until you receive production access to Amazon SES, you can only send to addresses you have verified. You can request production access here.)

  • Edit the sendmail_path config in your php.ini as follows:

sendmail_path = /path/to/ses-send-email.pl -k /path/to/aws-credentials -f someaddress@yourdomain.com -r

  • Restart/reload Apache and that’s it!

(Additional notes – The “From” address you set in your php.ini file will override any mail headers you set in php.  Sending will fail if you try to set the “From” header to an unverified address or when setting the “Reply-To” header at all in php.)


              			  				

16 responses so far

  • […] This post was mentioned on Twitter by Michael Jensen, Millcreek Systems. Millcreek Systems said: Switch your PHP site to use Amazon SES in a few simple steps: http://urlpie.com/eL3GC6 […]

  • It’s taking a long time to get approved for “production access” Much longer than I expected.

  • Orik says:

    I can’t seem to get this solution to work. Did anyone else experience problems with this method?

  • Jerry says:

    Tried this solution but does not work. I installe all the SES scripts unser /usr/sbin/SES and pointed the sendmail route in php.ini to that directory where the .pl file and aws-credentials are. Running the .pl file from the console sends the mail fine, but when I run @mail() from myPHP program it just won’t send the mail.

    Please advise and thanks in advance.

    Jerry

    • Verify that your php setting is correct by checking the output of ini_get(‘sendmail_path’) Also verify that the script can be run as the user that apache runs as (www-data on debian/ubuntu)

  • Steve Tran says:

    Thanks Mill,
    Great tip.
    You also need to soft link the file SES.pm to your perl path /etc/perl (in my Ubuntu box) as well. Otherwise you might encounter the error

    “Can’t locate SES.pm in @INC”

  • Vikash Kumar says:

    Somehow the mail is not going from php mail function. I am on ubuntu 11.04. Sendmail path is correct. The perl script has run permission for www-data users. And mail function is getting called. (Confirmed by enabling mail logging).

    I have production account. What could be the reason?

    • I’m not sure what would be causing that without having access to the system in question. You could try switching to the www-data user and running your sendmail_path command from the command line to see if it outputs any helpful error messages.

      • Vikash Kumar says:

        :) Looks like I had the “Can’t locate SES.pm in @INC” problem from www-data user. Anyway, I had it fixed by setting sendmail path as:

        sendmail_path = perl -I/path/to/ses/folder /path/to/ses-send-email.pl -k /path/to/aws-credentials -f someaddress@yourdomain.com -r

        Thanks a lot for your help and wonderful article. :)

  • […] configure your server to send email through it regardless of what platform your site is built in (my previous post was only relevant to PHP servers)  There are 3 main things you need to do to configure your Postfix server to relay email through […]

  • Andrew Weber says:

    This was extremely useful, thanks! Just had to make sure perl Digest/SHA.pm was installed.

  • John A. says:

    Thanks Michael, This solution worked well and was easy to follow. The SES script include path worked on one server with a simple edit to the $PATH, I had to add some symlinks for it to work on another server. That stuff was easy enough to figure out. All in all only a few hours of installation and testing to get it up.

    I only wish amazon made it more clear what all the approval steps were. We waited till the last minute wo do the final production approval step and it delayed us a bit.

    I’m going to look at your newer SES posts and see about getting one of the larger scale solutions in place.

  • Hi,

    I’m having a problem I havent been able to solve. I downloaded and unpacked the SES scripts, created an aws-credentials file chmod 600’ed it, and tested the script. I get this mess I cant make sense of from the ses-verify script. Anyone have any ideas?

    Thanks,
    Dave

    ./ses-verify-email-address.pl -k ./aws-credentials -v dave@doap.com
    String found where operator expected at SES.pm line 63, near “case ‘AWSAccessKeyId'”
    (Do you need to predeclare case?)
    String found where operator expected at SES.pm line 64, near “case ‘AWSSecretKey'”
    (Do you need to predeclare case?)
    “my” variable $data masks earlier declaration in same scope at SES.pm line 109.
    “my” variable $data masks earlier declaration in same scope at SES.pm line 145.
    “my” variable $request masks earlier declaration in same scope at SES.pm line 173.
    “my” variable $data masks earlier declaration in same scope at SES.pm line 175.
    “my” variable $sig masks earlier declaration in same scope at SES.pm line 178.
    “my” variable $signature masks earlier declaration in same scope at SES.pm line 180.
    “my” variable $request masks earlier declaration in same scope at SES.pm line 192.
    “my” variable $endpoint_name masks earlier declaration in same scope at SES.pm line 194.
    “my” variable $signature masks earlier declaration in same scope at SES.pm line 200.
    syntax error at SES.pm line 62, near “) {”
    syntax error at SES.pm line 64, near “case ‘AWSSecretKey'”
    Global symbol “$value” requires explicit package name at SES.pm line 64.
    syntax error at SES.pm line 65, near “else”
    syntax error at SES.pm line 81, near “}”
    syntax error at SES.pm line 97, near “}”
    syntax error at SES.pm line 126, near “}”
    syntax error at SES.pm line 168, near “}”
    syntax error at SES.pm line 187, near “}”
    syntax error at SES.pm line 209, near “}”
    SES.pm has too many errors.
    Compilation failed in require at ./ses-verify-email-address.pl line 27.
    BEGIN failed–compilation aborted at ./ses-verify-email-address.pl line 27.