aws:带附件的ses

时间:2014-05-23 12:25:03

标签: php amazon-web-services laravel-4 amazon-ses

我使用Laravel 4框架和AWS sdk进行SES。我可以使用sendEmail函数发送常规电子邮件。我希望能够将文件附加到电子邮件中,问题是我找不到怎么做。

是否可以使用sendEmail函数来附加文件或者我必须使用send_raw_email函数? (怎么做?)

这是我如何使用SES:

$msg['Source'] = Config::get('mail.mailSource');
$msg['Destination']['ToAddresses'][] = $_GET['email'];
$msg['Message']['Subject']['Data']      = "bla bla";
$msg['Message']['Body']['Text']['Data'] = 'bla bla';
$msg['Message']['Body']['Html']['Data'] = 'bla bla';

$ses = AWS::get('ses');
$ses->sendEmail($msg);

我在laravel中查看了AWS sdk并找到了对sendEmail函数有要求的数组,但没有附加文件的线索

'SendEmail' => array(
        'httpMethod' => 'POST',
        'uri' => '/',
        'class' => 'Aws\\Common\\Command\\QueryCommand',
        'responseClass' => 'SendEmailResponse',
        'responseType' => 'model',
        'parameters' => array(
            'Action' => array(
                'static' => true,
                'location' => 'aws.query',
                'default' => 'SendEmail',
            ),
            'Version' => array(......

2 个答案:

答案 0 :(得分:3)

我发现发送带附件的电子邮件(使用SES SERVICE)的唯一方法是使用SendRawEmail方法。

  $message = "To: ". $_GET['email'] ."\n";
            $message .= "From: ". $msg['Source'] ."\n";
            $message .= "Subject: Example SES mail (raw)\n";
            $message .= "MIME-Version: 1.0\n";
            $message .= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
            $message .= "\n\n";
            $message .= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
            $message .= 'Content-Type: text/plain; charset="utf-8"';
            $message .= "\n";
            $message .= "Content-Transfer-Encoding: 7bit\n";
            $message .= "Content-Disposition: inline\n";
            $message .= "\n";
            $message .= "Dear new tester,\n\n";
            $message .= "Attached is the file you requested.\n";
            $message .= "\n\n";
            $message .= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
            $message .= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>\n";
            $message .= 'Content-Type: application/zip; name="shell.zip"';
            $message .= "\n";
            $message .= "Content-Transfer-Encoding: base64\n";
            $message .= 'Content-Disposition: attachment; filename="file.png"';
            $message .= "\n";
            $message .= base64_encode( $attachedFile );
            $message .= "\n";
            $message .= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";

            $sendMsg['RawMessage']['Data']         = (string)base64_encode($message);
            $sendMsg['RawMessage']['Source']       = $msg['Source'];
            $sendMsg['RawMessage']['Destinations'] = $_GET['email'];

            $ses->SendRawEmail($sendMsg);

注意这一行:

$ message。=&#39;内容 - 处置:附件;文件名=&#34; file.png&#34;&#39 ;;

$ message。= base64_encode($ attachedFile);

答案 1 :(得分:0)

在AWS PHP的V3.x中,不应将base64_encode用于整个消息,而仅用于附件编码。因此,在V3.x中,请不要在下面进行BASE64 ENCODE $ myMessage:

    $myArraySES = [
        'Source'       => $myFromAdress,
        'Destinations' => $myArrayToAdresses,
        'RawMessage'   => [
            'Data' => $myMessage
        ]
    ];  

这是我使用AWS PHP SDK V3.x的PHP 7的工作解决方案。 此解决方案将通过AWS SES发送带有一个附件的HTML电子邮件:

use Aws\Ses\SesClient;

$gAWS_SES_client = SesClient::factory(array(
    'version'=> 'latest',
    'region' => 'eu-west-1',
    'credentials' => array(
        'key' => 'my_secret_key',
        'secret'  => 'my_secret_pw'
  ) 
));

function sendRAWEmailViaSES($myArrayToAdresses, $myFromAdress, $mySubject, $myHTMLBody, $myFilePath){
    global $gAWS_SES_client;

    echo "\n<BR>Now in sendRAWEmailViaSES()<BR>\n";

    $myStringToAddresses = implode(",", $myArrayToAdresses);

    $myFileName = basename($myFilePath);;

    $myDataAttachedFile = file_get_contents($myFilePath);
    $myDataAttachedFile = chunk_split(base64_encode($myDataAttachedFile));  

    $myFileMimeInfo = finfo_open(FILEINFO_MIME_TYPE);
    $myFileMimeType = finfo_file($myFileMimeInfo, $myFilePath);     

    $mySeparator = md5(time());
    $mySeparator_multipart = md5($mySubject . time());      

    $myMessage = "";

    $myMessage .= "MIME-Version: 1.0\n";

    $myMessage .= "To: ".$myStringToAddresses."\n"; 
    $myMessage .= "From:".$myFromAdress."\n";   
    $myMessage .= "Subject:".$mySubject."\n";

    $myMessage .= "Content-Type: multipart/mixed; boundary=\"".$mySeparator_multipart."\"\n";
    $myMessage .= "\n--".$mySeparator_multipart."\n";

    $myMessage .= "Content-Type: multipart/alternative; boundary=\"".$mySeparator."\"\n";
    $myMessage .= "\n--".$mySeparator."\n";

    $myMessage .= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $myMessage .= "\n".$myHTMLBody."\n";
    $myMessage .= "\n--".$mySeparator."--\n";

    $myMessage .= "--".$mySeparator_multipart."\n";
    $myMessage .= "Content-Type: ".$myFileMimeType."; name=\"".$myFileName."\"\n";
    $myMessage .= "Content-Disposition: attachment; filename=\"".$myFileName."\"\n";
    $myMessage .= "Content-Transfer-Encoding: base64\n\n";
    $myMessage .= $myDataAttachedFile."\n";
    $myMessage .= "--".$mySeparator_multipart."--";

    //echo "\n<BR>Message:<BR>\n";
    //print_r($myMessage);
    //echo "<BR>\n";        


    $myArraySES = [
        'Source'       => $myFromAdress,
        'Destinations' => $myArrayToAdresses,
        'RawMessage'   => [
            'Data' => $myMessage
        ]
    ];  

    echo "\n<HR>Trying to send mail via AWS SES <BR>\n";


    try{
        $myResult = true;
        $myAPIResult = $gAWS_SES_client->sendRawEmail($myArraySES);

        //save the MessageId which can be used to track the request
        //$myMessageArrayID = $myAPIResult->get('MessageId');
        //echo("MessageId: $msg_id");

        //view sample output
        echo "\n<BR>SES Result:<BR>\n";
        print_r($myAPIResult);
        echo "<BR>\n";

    } catch (Exception $myObjError) {
        //An error happened and the email did not get sent
        $myErrorInfo = $myObjError->getMessage();

        echo "\n<BR>*** SES ERROR:<BR>\n";
        print_r($myErrorInfo);
        echo "<BR>\n";      

        $myfile = fopen("ses_send_error.txt", "w");
        fwrite($myfile, $myErrorInfo);
        fclose($myfile);

    }   


}