{"id":27,"date":"2011-04-28T09:47:59","date_gmt":"2011-04-28T16:47:59","guid":{"rendered":"https:\/\/www.alexkorn.com\/blog\/?p=27"},"modified":"2013-06-27T21:55:50","modified_gmt":"2013-06-28T04:55:50","slug":"sending-email-aws-php-with-amazonses","status":"publish","type":"post","link":"https:\/\/www.alexkorn.com\/blog\/2011\/04\/sending-email-aws-php-with-amazonses\/","title":{"rendered":"Sending email from AWS in PHP with AmazonSES"},"content":{"rendered":"<p><i>Update 2013\/06\/27: I&#8217;ve updated some of the code to match new SES interfaces.<\/i><\/p>\n<p>\nFor those who have sent emails in <a href=\"http:\/\/www.php.net\/\">PHP<\/a>, the following code should look quite familiar:\n<\/p>\n<pre lang=\"php\">mail($to, $subject, $message);<\/pre>\n<p>\nUnfortunately, this will silently fail on <a href=\"http:\/\/aws.amazon.com\/\">Amazon Web Services<\/a>. But you do need to send those emails, right? Well, it&#8217;s pretty easy to get emails sent from AWS with <a href=\"http:\/\/aws.amazon.com\/ses\/\">Amazon Simple Email Service (Amazon SES)<\/a>. However, in my research on this, I found several false leads and no good tutorials, so here goes: step by step, how to send emails with AWS.<\/p>\n<p>First step: <a href=\"http:\/\/aws-portal.amazon.com\/gp\/aws\/developer\/subscription\/index.html?ie=UTF8&#038;productCode=AmazonSES\">sign up<\/a>.\n<\/p>\n<h2>AWS SDK for PHP<\/h2>\n<p>\nNext, install the source code code for <a href=\"https:\/\/github.com\/amazonwebservices\/aws-sdk-for-php\">AWS SDK for PHP<\/a>. Run the following in the directory where you store external libraries (using <code>sudo<\/code> when needed):\n<\/p>\n<pre>\r\nyum install git\r\ngit clone git:\/\/github.com\/amazonwebservices\/aws-sdk-for-php.git AWSSDKforPHP\r\n<\/pre>\n<p>\nNow that you have the the AWS SDK on your server, include <code> AWSSDKforPHP\/sdk.class.php<\/code> into your application. You now have access to the <code>AmazonSES<\/code> class.\n<\/p>\n<p>The following is a basic function to emulate PHP&#8217;s <code>mail()<\/code>.<\/p>\n<pre lang=\"php\">\r\nfunction amazonSesEmail($to, $subject, $message)\r\n{\r\n    $amazonSes = new AmazonSES(array(\r\n        'key' => AWS_KEY,\r\n        'secret' => AWS_SECRET_KEY\r\n    ));\r\n\r\n    $response = $amazonSes->send_email(AWS_SES_FROM_EMAIL,\r\n        array('ToAddresses' => array($to)),\r\n        array(\r\n            'Subject.Data' => $subject,\r\n            'Body.Text.Data' => $message,\r\n        )\r\n    );\r\n    if (!$response->isOK())\r\n    {\r\n        \/\/ handle error\r\n    }\r\n}\r\n<\/pre>\n<p>\nNote that you need to define the AWS_KEY and AWS_SECRET_KEY for your application. You can find these by going to your <a href=\"https:\/\/aws-portal.amazon.com\/gp\/aws\/developer\/account\/index.html?ie=UTF8&#038;action=access-key\">AWS Security Credentials page<\/a> and looking under &#8220;Access Keys&#8221;.\n<\/p>\n<h2>Validating the sending email address<\/h2>\n<p><a name=\"backtoperlmodulesasterisk\"><\/a><\/p>\n<p>\nNext, define <code>AWS_SES_FROM_EMAIL<\/code> to be the email address from which you want to send emails, e.g. <code>no-reply@example.com<\/code>. If you try to send an email at this point, you&#8217;ll get a <code>MessageRejected<\/code> error with the message &#8220;Email address is not verified.&#8221; Several pages I visited recommended I download the <a href=\"http:\/\/aws.amazon.com\/code\/Amazon-SES\/8945574369528337\">Amazon SES Scripts<\/a> (Perl), but don&#8217;t do it! You&#8217;ll need to subject yourself to a world of pain in the form of installing Perl modules<a href=\"#perlmodules\">*<\/a> (and probably Perl too, given that you&#8217;re reading a post on PHP!).\n<\/p>\n<p>\nInstead, you can actually use <code>AmazonSES<\/code> to verify email addresses in PHP.\n<\/p>\n<pre lang=\"php\">\r\n$amazonSes = new AmazonSES(array(\r\n    'key' => AWS_KEY,\r\n    'secret' => AWS_SECRET_KEY\r\n));\r\n$amazonSes->verify_email_address($fromEmailAddress);\r\n<\/pre>\n<p>\nMuch easier than the Perl script shenanigans. This will send an email to your sending email address with a link to verify that you own that email address. Do the same for your recipient addresses (since you&#8217;re in <a href=\"http:\/\/aws.amazon.com\/ses\/faqs\/#9\">sandbox mode, which has restrictions<\/a> including the requirement to validate your recipient address). Verify each address and you should be good to go to send emails.\n<\/p>\n<h2>Setting up production access<\/h2>\n<p>\nAt this point you&#8217;ll be able to send a couple test emails, but the volume is fairly limited. <a href=\"https:\/\/aws-portal.amazon.com\/gp\/aws\/html-forms-controller\/contactus\/SESAccessRequest\">Request production access<\/a> and you should be granted the ability to send many more emails and not need to verify recipient email addresses.\n<\/p>\n<hr>\n<h2>Reference<\/h2>\n<p>\nVisit the <a href=\"http:\/\/docs.amazonwebservices.com\/AWSSDKforPHP\/latest\/index.html\">AWS SDK for PHP reference page<\/a> for much more information on how AWS SDK for PHP works.\n<\/p>\n<p><a name=\"perlmodules\"><\/a><\/p>\n<h2>Amazon SES Scripts (Perl)<\/h2>\n<p>\nIf you do want to install the <a href=\"http:\/\/aws.amazon.com\/code\/Amazon-SES\/8945574369528337\">Amazon SES Perl scripts<\/a> and are getting an error along the lines of &#8220;Can&#8217;t locate XML\/LibXML.pm in @INC&#8230;&#8221;, you can use <code>yum<\/code> to install all of the necessary packages to get them to run.\n<\/p>\n<pre>\r\nyum -y install perl-Digest-SHA perl-URI perl-libwww-perl perl-MIME-tools perl-Crypt-SSLeay perl-XML-LibXML\r\n<\/pre>\n<p>\nThanks to <a href=\"https:\/\/forums.aws.amazon.com\/thread.jspa?messageID=219753#219753\">this post<\/a> for that hint.\n<\/p>\n<p style=\"font-size:x-small;\">\n(<a href=\"#backtoperlmodulesasterisk\">^ Back up<\/a>)<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Update 2013\/06\/27: I&#8217;ve updated some of the code to match new SES interfaces. For those who have sent emails in PHP, the following code should look quite familiar: mail($to, $subject, $message); Unfortunately, this will silently fail on Amazon Web Services. But you do need to send those emails, right? Well, it&#8217;s pretty easy to get [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,15,16],"tags":[6],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-aws","category-hosting","category-tutorials","tag-php"],"_links":{"self":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}