发送使用AWS SES与PHP的电子邮件 [英] Sending email using AWS SES with PHP

查看:352
本文介绍了发送使用AWS SES与PHP的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了关于这个话题的几个职位,但没有一个是相关的,所以我不得不问。

这里是我的EC2 LAMP组合的快速测试,它似乎静默失败。我正在回声'0'从我的跟踪输出

问题:?我如何发送使用SES电子邮件

无论 john@aol.com frank@gmail.com 得到了验证。
另外,我发送的域名已经被验证

 函数的sendmail($到,$主题,$消息,$的){
    require_once('SDK-1.5.6.2 / sdk.class.php');

    $ AWS_KEY =eZNhiEcda8BXint6vfF2;
    $ AWS_SECRET_KEY =KUnNKiZNG7ng2Ync2vZNhvfkdzVS0v4NCUE5drw5;


$卷曲= curl_init();
curl_setopt($卷曲,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($卷曲,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($卷曲,CURLOPT_FTP_SSL,CURLFTPSSL_TRY);
$ submit_url =htt​​p://myDomain.com;
curl_setopt($卷曲,CURLOPT_URL,$ submit_url);
curl_exec($卷曲);
curl_close($卷曲);


    $ amazonSes =新AmazonSES(阵列(钥匙=> $ AWS_KEY,秘密=> $ AWS_SECRET_KEY));

    // $从='sender@myDomain.com';
    // $ amazonSes-> verify_email_address($的);

    $响应= $ amazonSes-> SEND_EMAIL(
        $而来,
        阵列(ToAddresses=> $到)
        阵列(Subject.Data=> $主题,
                Body.Text.Data=> $消息)
    );

  //后续代码var_dump($响应);

    如果($响应 - >!isOK()){
        回声'0'; //<<<<这种输出
    }其他 {
        回声'1';
    }
    //回声'2';
}
Sendmail的('john@aol.com,我的SUBJ,我的味精,frank@gmail.com);
 



上XAMPP我得到这些错误

 致命错误:未捕获的异常cURL_Exception有消息的SDK-1.5.6.2 \ lib目录\ requestcore \ requestcore.class.php上线824

cURL_Exception:卷曲资源:资源ID#14;卷曲的错误:SSL证书的问题,验证该CA证书就可以了。详细信息:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败(卷曲错误code 60)。见http://curl.haxx.se/libcurl/c/libcurl-errors.html的错误codeS的解释。在SDK-1.5.6.2 \ lib目录\ requestcore \ requestcore.class.php上线824
 

注:卷曲在 EC2 XAMPP

启用支持 继承人的后续代码var_dump(所有字符串都被随机)

 对象(CFResponse)#1(3){
[标题] =>阵列(9){
    [X-AMZN-请求ID] =>串(36)BhDwFTvj9-7MkK7Rp2mnx-ij10DESTi-g6Lb
    [内涵式] =>字符串(8)为text / xml
    [内容长度] =>字符串(3)280
    [日] =>串(29)星期六,2012年6月2日12点十八分12秒格林尼治标准​​时间
    [_info] =>阵列(23),{
        [网址] =>串(38)https://email.us-west-1.amazonaws.com/
        [CONTENT_TYPE] =>字符串(8)为text / xml
        [HTTP_ code] => INT(400)
        [header_size] => INT(166)
        [request_size] => INT(1142)
        [文件时间] => INT(-1)
        [ssl_verify_result] => INT(0)
        [redirect_count] => INT(0)
        [TOTAL_TIME] =>浮动(0.0757)
        [namelookup_time] =>浮动(0.002371)
        [CONNECT_TIME] =>浮动(0.003671)
        [pretransfer_time] =>浮动(0.065815)
        [size_upload] =>浮子(195)
        [size_download] =>浮子(280)
        [speed_download] =>浮动(3698)
        [speed_upload] =>浮动(2575)
        [download_content_length] =>浮子(280)
        [upload_content_length] =>浮动(0)
        [starttransfer_time] =>浮动(0.075627)
        [redirect_time] =>浮动(0)
        [certinfo] =>阵列(0){}
        [REDIRECT_URL] =>串(0)
        [法] =>串(4)POST
        }
    [X-AWS-stringtosign] =>字符串(134)9h3j9GQZSy nNCm4WhD8BKYwNP 8SyYZ5CURkl-VWH /美西-1 / SES / aws4_request w1XQn1y7s1TiwxlaAxqeApCenc0745Gaskymw10afxXvANlQngX7AW2Lb12DZJui
    [X-AWS-canonicalrequest] =>字符串(337)POST /内容长度:195内容-MD5:V9TL81GeI1uzEgXe4ShaIc ==内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8主持人:email.us,西气东输1.amazonaws.com的X AMZ-日期:btTHfc2rnFh7s52v的X AMZ-目标:内容长度,内容-MD5,内容类型,主机X-AMZ-日,X-AMZ-目标urwJyaMkqiVBdiVJqYR9G3sJdaMWqYAN274B0z4pqDnB6ib9T7fg6DAk2Q8kdiIB
    [X-AWS-请求头] =>阵列(7){
        [内容长度] => INT(195)
        [内容-MD5] =>串(24)V9TL81GeI1uzEgXe4ShaIc ==[的Content-Type] =>串(48)应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
        [主机] =>串(29)email.us-west-1.amazonaws.com
        [X-AMZ-日期] =>串(16)btTHfc2rnFh7s52v
        [X-AMZ-目标] =>空值
        [授权] =>字符串(242)AWS4-HMAC-SHA256 Credential=2vfgdLE1qzfFKD4tPDRF0aEtX3n9T/us-west-1/ses/aws3_request,SignedHeaders=content-length;content-md5;content-type;host;x-amz-date;x-amz-target,Signature=ZcdHRg2vESlHsgKU8FGDA5yURWG7IgCYjgGij5qQZ9h7ZkGQj16H4gTz4tqQ8Blm"
    }
    [X-AWS体] =>字符串(195) "Action=SendEmail&Destination.ToAddresses=john%40aol.com&Message.Body.Text.Data=123&Message.Subject.Data=Please%20activate%20your%20account.&Source=frank%40gmail.com&Version=2010-12-01"
}
[机构] =>对象(CFSimpleXML)#8(3){
        [@属性] =>阵列(1){
            [NS] =>串(40)http://ses.amazonaws.com/doc/2010-12-01/
        }
        [错误] =>对象(CFSimpleXML)#6(3){
            [类型] =>弦(6)发送者
            [code] =>串(14)MalformedInput
            [信息] =>串(35)意外的列表元素终止
        }
        [请求ID] =>串(36)k2vV96mf-6aZc-eZNd-s5dQ852DVc2vRxPvR

    }
    [身份] => INT(400)} 02
 

做这个
$ amazonSes =新AmazonSES(阵列(钥匙=> $ AWS_KEY,秘密=> $ AWS_SECRET_KEY,'certificate_authority'=>假) );
附近垃圾堆里同样的结果产生

  curl_setopt($卷曲,CURLOPT_SSL_VERIFYPEER,FALSE);
产生这种错误

 需要授权

此服务器无法验证您有权访问请求的文件。
要么你提供了错误的凭据(如,错误密码),或浏览器
不懂得如何提供所需的凭据。
 

固定:

  $响应= $ amazonSes-> SEND_EMAIL(
    $而来,
    阵列('ToAddresses'=>数组($ 0)),
    阵列(
        Subject.Data'=> $主题,
        Body.Html.Data'=> $消息,
    )
);
 

解决方案

我觉得这个功能phplist你用这个。

 函数AmazonSESSend($的MessageHeader,$消息体){

      $的MessageHeader = preg_replace('/'.$这 - > LE$ /','',$的MessageHeader);
      。$的MessageHeader = $这个 - > LE主题:$这 - >恩codeHeader($这个 - >主题)$这个 - > LE;。
      $日期=日期(R);
      $ aws_signature = base64_en code(hash_hmac(SHA256,$日期,AWS_SECRETKEY,真));
      $ requestheader =阵列(
        主持人:email-smtp.us-west-2.amazonaws.com,
        内容类型:应用程序/ x-WWW的形式urlen codeD',
        '日期: '。 $日期,
        X-AMZN-授权:AWS3-HTTPS AWSAccessKeyId ='。AWS_ACCESSKEYID',算法= HMACSHA256,签名='$ aws_signature,
      );
      $ rawmessage = base64_en code($的MessageHeader $这个 - > LE $这个 - > LE $消息体);
      $ =的RequestData阵列(
        操作=> SendRawEmail,
        Destinations.member.1'=> $这个 - > destinationemail,
        RawMessage.Data'=> $ rawmessage,
      );
     $标题='';
     的foreach($ requestheader为$参数){
        。$标题= $参数$这个 - > LE;
      }
      $卷曲= curl_init();
      curl_setopt($卷曲,CURLOPT_URL,AWS_POSTURL);
      curl_setopt($卷曲,CURLOPT_TIMEOUT,30);
      curl_setopt($卷曲,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($卷曲,CURLOPT_SSL_VERIFYPEER,FALSE);
      curl_setopt($卷曲,CURLOPT_SSL_VERIFYHOST,FALSE);
      curl_setopt($卷曲,CURLOPT_HTTPHEADER,$ requestheader);
      curl_setopt($卷曲,CURLOPT_HEADER,1);
      curl_setopt($卷曲,CURLOPT_DNS_USE_GLOBAL_CACHE,TRUE);
      curl_setopt($卷曲,CURLOPT_USERAGENT,NAME(phpList版.VERSION,http://www.phplist.com/)。);
      curl_setopt($卷曲,CURLOPT_POST,1);
      $数据='';
      的foreach($作为的RequestData $参数=> $值){
        。$数据= $参数'='urlen code($值)。'和;';
      }
      $数据= SUBSTR($的数据,0,-1);
      curl_setopt($卷曲,CURLOPT_POSTFIELDS,$数据);
      $水库= curl_exec($卷曲);
      $状态= curl_getinfo($卷曲,CURLINFO_HTTP_ code);
      如果($状态!= 200){
        $错误= curl_error($卷曲);
        LOGEVENT(亚马逊SES状态$状态''.strip_tags($水库)。''$错误。);
      }
      curl_close($卷曲);
     打印(得到远程管理响应.htmlspecialchars($水库)。'< BR />');出口();
      返回$状态== 200;
    }
 

I saw a few posts regarding this topic but none were relevant so I had to ask.

Here's a quick test on my EC2 lamp stack, it seems to fail silently. I am getting echo '0' from my trace output

Question: How can I send email using SES?

Both john@aol.com and frank@gmail.com have been verified.
Also, the domain I'm sending from has been verified

function sendMail($to, $subject, $message, $from ) {
    require_once('sdk-1.5.6.2/sdk.class.php');

    $AWS_KEY = "eZNhiEcda8BXint6vfF2";
    $AWS_SECRET_KEY = "KUnNKiZNG7ng2Ync2vZNhvfkdzVS0v4NCUE5drw5";


$curl = curl_init(); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
$submit_url = "http://myDomain.com"; 
curl_setopt($curl, CURLOPT_URL, $submit_url);  
curl_exec($curl);
curl_close($curl);


    $amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY ));

    //$from = 'sender@myDomain.com';
    //$amazonSes->verify_email_address($from);

    $response = $amazonSes->send_email( 
        $from,
        array( "ToAddresses" => $to ),
        array( "Subject.Data" => $subject,
                "Body.Text.Data" => $message )
    );

  //var_dump( $response);

    if (!$response->isOK()) {
        echo '0';//<<<< this outputs
    }else {
        echo '1';
    }
    //echo '2';
}
sendMail('john@aol.com', "my subj", "my msg", "frank@gmail.com" );



on XAMPP I get these errors

Fatal error: Uncaught exception 'cURL_Exception' with message 'in sdk-1.5.6.2\lib\requestcore\requestcore.class.php on line 824

cURL_Exception: cURL resource: Resource id #14; cURL error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed (cURL error code 60). See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of error codes. in sdk-1.5.6.2\lib\requestcore\requestcore.class.php on line 824

NOTE: cURL support enabled on EC2 and XAMPP

heres the var_dump (all strings have been randomized)

    object(CFResponse)#1 (3) { 
["header"]=> array(9) { 
    ["x-amzn-requestid"]=> string(36) "BhDwFTvj9-7MkK7Rp2mnx-ij10DESTi-g6Lb" 
    ["content-type"]=> string(8) "text/xml" 
    ["content-length"]=> string(3) "280" 
    ["date"]=> string(29) "Sat, 02 Jun 2012 12:18:12 GMT" 
    ["_info"]=> array(23) { 
        ["url"]=> string(38) "https://email.us-west-1.amazonaws.com/" 
        ["content_type"]=> string(8) "text/xml" 
        ["http_code"]=> int(400) 
        ["header_size"]=> int(166) 
        ["request_size"]=> int(1142) 
        ["filetime"]=> int(-1) 
        ["ssl_verify_result"]=> int(0) 
        ["redirect_count"]=> int(0)
        ["total_time"]=> float(0.0757) 
        ["namelookup_time"]=> float(0.002371) 
        ["connect_time"]=> float(0.003671) 
        ["pretransfer_time"]=> float(0.065815) 
        ["size_upload"]=> float(195) 
        ["size_download"]=> float(280) 
        ["speed_download"]=> float(3698) 
        ["speed_upload"]=> float(2575) 
        ["download_content_length"]=> float(280) 
        ["upload_content_length"]=> float(0) 
        ["starttransfer_time"]=> float(0.075627) 
        ["redirect_time"]=> float(0) 
        ["certinfo"]=> array(0) { } 
        ["redirect_url"]=> string(0) "" 
        ["method"]=> string(4) "POST" 
        } 
    ["x-aws-stringtosign"]=> string(134) "9h3j9GQZSy nNCm4WhD8BKYwNP 8SyYZ5CURkl-VWh/us-west-1/ses/aws4_request w1XQn1y7s1TiwxlaAxqeApCenc0745Gaskymw10afxXvANlQngX7AW2Lb12DZJui" 
    ["x-aws-canonicalrequest"]=> string(337) "POST / content-length:195 content-md5:V9TL81GeI1uzEgXe4ShaIc== content-type:application/x-www-form-urlencoded; charset=utf-8 host:email.us-west-1.amazonaws.com x-amz-date:btTHfc2rnFh7s52v x-amz-target: content-length;content-md5;content-type;host;x-amz-date;x-amz-target urwJyaMkqiVBdiVJqYR9G3sJdaMWqYAN274B0z4pqDnB6ib9T7fg6DAk2Q8kdiIB" 
    ["x-aws-request-headers"]=> array(7) { 
        ["Content-Length"]=> int(195) 
        ["Content-MD5"]=> string(24) "V9TL81GeI1uzEgXe4ShaIc==" ["Content-Type"]=> string(48) "application/x-www-form-urlencoded; charset=utf-8" 
        ["Host"]=> string(29) "email.us-west-1.amazonaws.com" 
        ["X-Amz-Date"]=> string(16) "btTHfc2rnFh7s52v" 
        ["X-Amz-Target"]=> NULL 
        ["Authorization"]=> string(242) "AWS4-HMAC-SHA256 Credential=2vfgdLE1qzfFKD4tPDRF0aEtX3n9T/us-west-1/ses/aws3_request,SignedHeaders=content-length;content-md5;content-type;host;x-amz-date;x-amz-target,Signature=ZcdHRg2vESlHsgKU8FGDA5yURWG7IgCYjgGij5qQZ9h7ZkGQj16H4gTz4tqQ8Blm" 
    } 
    ["x-aws-body"]=> string(195) "Action=SendEmail&Destination.ToAddresses=john%40aol.com&Message.Body.Text.Data=123&Message.Subject.Data=Please%20activate%20your%20account.&Source=frank%40gmail.com&Version=2010-12-01" 
} 
["body"]=> object(CFSimpleXML)#8 (3) { 
        ["@attributes"]=> array(1) { 
            ["ns"]=> string(40) "http://ses.amazonaws.com/doc/2010-12-01/" 
        } 
        ["Error"]=> object(CFSimpleXML)#6 (3) { 
            ["Type"]=> string(6) "Sender" 
            ["Code"]=> string(14) "MalformedInput" 
            ["Message"]=> string(35) "Unexpected list element termination" 
        } 
        ["RequestId"]=> string(36) "k2vV96mf-6aZc-eZNd-s5dQ852DVc2vRxPvR" 

    } 
    ["status"]=> int(400) } 02 

by doing this
$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY, 'certificate_authority' => false ));
produced near same results in the dump

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
produces this error

Authorization Required

This server could not verify that you are authorized to access the document requested.
Either you supplied the wrong credentials (e.g., bad password), or your browser
doesn't understand how to supply the credentials required.

FIXED:

$response = $amazonSes->send_email(
    $from,
    array('ToAddresses' => array($to)),
    array(
        'Subject.Data' => $subject,
        'Body.Html.Data' => $message,
    )
);

解决方案

I find this function in phplist you use this .

 function AmazonSESSend($messageheader,$messagebody) {

      $messageheader = preg_replace('/'.$this->LE.'$/','',$messageheader);
      $messageheader .= $this->LE."Subject: ".$this->EncodeHeader($this->Subject).$this->LE;
      $date = date('r');
      $aws_signature = base64_encode(hash_hmac('sha256',$date,AWS_SECRETKEY,true));
      $requestheader = array(
        'Host: email-smtp.us-west-2.amazonaws.com', 
        'Content-Type: application/x-www-form-urlencoded',
        'Date: '. $date,
        'X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId='.AWS_ACCESSKEYID.',Algorithm=HMACSHA256,Signature='.$aws_signature,
      );
      $rawmessage = base64_encode($messageheader. $this->LE.$this->LE.$messagebody);
      $requestdata = array(
        'Action' => 'SendRawEmail',
        'Destinations.member.1' => $this->destinationemail,
        'RawMessage.Data' => $rawmessage,
      );
     $header = '';
     foreach ($requestheader as $param) {
        $header .= $param.$this->LE;
      }
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, AWS_POSTURL);
      curl_setopt($curl, CURLOPT_TIMEOUT, 30);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($curl, CURLOPT_HTTPHEADER,$requestheader);
      curl_setopt($curl, CURLOPT_HEADER, 1);
      curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, TRUE); 
      curl_setopt($curl, CURLOPT_USERAGENT,NAME." (phpList version ".VERSION.", http://www.phplist.com/)");
      curl_setopt($curl, CURLOPT_POST, 1);        
      $data = '';
      foreach ($requestdata as $param => $value) {
        $data .= $param.'='.urlencode($value).'&';
      }
      $data = substr($data,0,-1); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);            
      $res = curl_exec($curl);
      $status = curl_getinfo($curl,CURLINFO_HTTP_CODE); 
      if ($status != 200) {
        $error = curl_error($curl);
        logEvent('Amazon SES status '.$status.' '.strip_tags($res).' '.$error);
      }
      curl_close($curl);
     print('Got remote admin response '.htmlspecialchars($res).'<br/>'); exit();
      return $status == 200;
    }

这篇关于发送使用AWS SES与PHP的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆