2

我看到了一些关于这个主题的帖子,但没有一个是相关的,所以我不得不问。

这是对我的 EC2 灯组的快速测试,它似乎默默地失败了。我echo '0'从跟踪输出中得到

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

两者均john@aol.comfrank@gmail.com验证。
另外,我发送的域已经过验证

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" );



在 XAMPP 我得到这些错误

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

注意:在EC2XAMPP上启用 cURL 支持

是 var_dump(所有字符串都已随机化)

    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 

通过这样做
$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY, 'certificate_authority' => false ));
在转储中产生几乎相同的结果

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
会产生此错误

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.

固定的:

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

3 回答 3

1

我在 phplist 中找到了这个函数,你使用这个。

 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;
    }
于 2014-12-25T17:36:10.043 回答
1

看起来 cURL 无法验证证书是否可信。

您需要将 CA 和任何中间证书安装到 cURL 使用的证书存储库(取决于平台和安装)。

您还可以尝试按照Serg ikS在评论中的建议禁用证书验证。我不确定这如何与 AmazonSES SDK 一起使用,但他们建议的方式听起来像是合适的解决方案。使用通用curl_*功能,您curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);应该消除错误。

请注意,禁用证书验证是危险的,因为这意味着您容易受到中间人攻击。

于 2012-06-02T03:19:07.400 回答
0

您可能想尝试这个课程来帮助您:

http://www.orderingdisorder.com/aws/ses/

我发现它非常容易开箱即用,并提供了大多数正常功能。

于 2013-11-26T00:51:52.953 回答