Created
May 5, 2012 12:31
-
-
Save 1999/2602021 to your computer and use it in GitHub Desktop.
AWS SES php example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set( 'Europe/Moscow' ); | |
ini_set( 'memory_limit', '1024M' ); | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, 'https://email.us-east-1.amazonaws.com' ); | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_HEADER, true ); | |
$date = date( 'r' ); | |
$sign = base64_encode( hash_hmac( 'sha256', $date, AWS_SECRET_KEY, true ) ); | |
$postData = array( | |
'Action' => 'SendEmail', | |
'Source' => encodeEmailUTF8( $data->from_person ) . '<' . $data->from_email . '>', | |
'Destination.ToAddresses.member.1' => $data->to, | |
'Message.Subject.Data' => encodeEmailUTF8( $data->subject ), | |
'Message.Subject.Charset' => 'UTF-8', | |
'Message.Body.Html.Data' => $logoHeader . $data->message, | |
'Message.Body.Html.Charset' => 'UTF-8' | |
); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postData ) ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( | |
'X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=' . AWS_ACCESS_KEY . ', Algorithm=HmacSHA256, Signature=' . $sign, | |
'Date: ' . $date, | |
'Content-Type: application/x-www-form-urlencoded' | |
) ); | |
$response = curl_exec( $ch ); | |
if ( curl_getinfo( $ch, CURLINFO_HTTP_CODE ) != '200' ) { | |
// в $response находится JSON ошибки | |
// $headers = 'From: ' . $data->from_person . ' <' . $data->from_email . '>' . "\n"; | |
// $headers .= 'Content-type: text/html; charset=utf-8' . "\n"; | |
// mb_send_mail( $data->to, $data->subject, $data->message, $headers ); | |
} | |
function encodeEmailUTF8( $str ) { | |
return '=?utf-8?B?' . base64_encode( $str ) . '?='; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment