Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

symfony - Sending email with attachment using amazon SES

I am trying to send an email that contains an attachment (a pdf file) using the amazon SES API.

I am using Symfony2 so I included the AmazonWebServiceBundle in my project. I can send a simple email (that has no attachment) pretty easily with the following code:

$ses = $this->container->get('aws_ses');
$recip = array("ToAddresses"=>array("[email protected]"));
$message = array("Subject.Data"=>"My Subject","Body.Text.Data"=>"My Mail body");
$result = $ses->send_email("[email protected]",$recip, $message);

Unfortunately, to send an email with attachment, i need to use the sendRawEmail function instead of the send_email function.

I am unable to find how to use this function, could anybody help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it is a pain to send emails with attachments using SES. Maybe this will help someone else out who is still struggling with it. I've written a simple class that helps simplify the call to sendRawEmail.

Usage:

$subject_str = "Some Subject";
$body_str = "<strong>Some email body</strong>";
$attachment_str = file_get_contents("/htdocs/test/sample.pdf");

//send the email
$result = SESUtils::deliver_mail_with_attachment(
    array('[email protected]', '[email protected]'),       
    $subject_str, $body_str, 'sender@verifiedbyaws', 
    $attachment_str);

//now handle the $result if you wish

Class:

<?php
require 'AWSSDKforPHP/aws.phar';
use AwsSesSesClient;

/**
 * SESUtils is a tool to make it easier to work with Amazon Simple Email Service
 * Features:
 * A client to prepare emails for use with sending attachments or not
 * 
 * There is no warranty - use this code at your own risk.  
 * @author sbossen 
 * http://right-handed-monkey.blogspot.com
 */
class SESUtils {

    const version = "1.0";
    const AWS_KEY = "your_aws_key";
    const AWS_SEC = "your_aws_secret";
    const AWS_REGION = "us-east-1";
    const BOUNCER = "[email protected]";  //if used, this also needs to be a verified email
    const LINEBR = "
";
    const MAX_ATTACHMENT_NAME_LEN = 60;

    /**
     * Usage
     * $result = SESUtils::deliver_mail_with_attachment(array('[email protected]', '[email protected]'), $subject_str, $body_str, 'validatedsender@aws', $attachment_str);
     * use $result->success to check if it was successful
     * use $result->message_id to check later with Amazon for further processing
     * use $result->result_text to look for error text if the task was not successful
     * 
     * @param type $to - individual address or array of email addresses
     * @param type $subject - UTF-8 text for the subject line
     * @param type $body - Text for the email
     * @param type $from - email address of the sender (Note: must be validated with AWS as a sender)
     * @return ResultHelper
     */
    public static function deliver_mail_with_attachment($to, $subject, $body, $from, &$attachment = "", $attachment_name = "doc.pdf", $attachment_type = "Application/pdf", $is_file = false, $encoding = "base64", $file_arr = null) {
        $result = new ResultHelper();
        //get the client ready
        $client = SesClient::factory(array(
                    'key' => self::AWS_KEY,
                    'secret' => self::AWS_SEC,
                    'region' => self::AWS_REGION
        ));
        //build the message
        if (is_array($to)) {
            $to_str = rtrim(implode(',', $to), ',');
        } else {
            $to_str = $to;
        }
        $msg = "To: $to_str".self::LINEBR;
        $msg .="From: $from".self::LINEBR;
        //in case you have funny characters in the subject
        $subject = mb_encode_mimeheader($subject, 'UTF-8');
        $msg .="Subject: $subject".self::LINEBR;
        $msg .="MIME-Version: 1.0".self::LINEBR;
        $msg .="Content-Type: multipart/alternative;".self::LINEBR;
        $boundary = uniqid("_Part_".time(), true); //random unique string
        $msg .=" boundary="$boundary"".self::LINEBR;
        $msg .=self::LINEBR;
        //now the actual message
        $msg .="--$boundary".self::LINEBR;
        //first, the plain text
        $msg .="Content-Type: text/plain; charset=utf-8".self::LINEBR;
        $msg .="Content-Transfer-Encoding: 7bit".self::LINEBR;
        $msg .=self::LINEBR;
        $msg .=strip_tags($body);
        $msg .=self::LINEBR;
        //now, the html text
        $msg .="--$boundary".self::LINEBR;
        $msg .="Content-Type: text/html; charset=utf-8".self::LINEBR;
        $msg .="Content-Transfer-Encoding: 7bit".self::LINEBR;
        $msg .=self::LINEBR;
        $msg .=$body;
        $msg .=self::LINEBR;
        //add attachments
        if (!empty($attachment)) {
            $msg .="--$boundary".self::LINEBR;
            $msg .="Content-Transfer-Encoding: base64".self::LINEBR;
            $clean_filename = mb_substr($attachment_name, 0, self::MAX_ATTACHMENT_NAME_LEN);
            $msg .="Content-Type: $attachment_type; name=$clean_filename;".self::LINEBR;
            $msg .="Content-Disposition: attachment; filename=$clean_filename;".self::LINEBR;
            $msg .=self::LINEBR;
            $msg .=base64_encode($attachment);
            //only put this mark on the last entry
            if (!empty($file_arr))
                $msg .="==".self::LINEBR;
            $msg .="--$boundary";
        }
        if (!empty($file_arr) && is_array($file_arr)) {
            foreach ($file_arr as $file) {
                $msg .="Content-Transfer-Encoding: base64".self::LINEBR;
                $clean_filename = mb_substr($attachment_name, 0, self::MAX_ATTACHMENT_NAME_LEN);
                $msg .="Content-Type: application/octet-stream; name=$clean_filename;".self::LINEBR;
                $msg .="Content-Disposition: attachment; filename=$clean_filename;".self::LINEBR;
                $msg .=self::LINEBR;
                $msg .=base64_encode($attachment);
                //only put this mark on the last entry
                if (!empty($file_arr))
                    $msg .="==".self::LINEBR;
                $msg .="--$boundary";
            }
        }
        //close email
        $msg .="--".self::LINEBR;

        //now send the email out
        try {
            $ses_result = $client->sendRawEmail(array(
                'RawMessage' => array('Data' => base64_encode($msg))), array('Source' => $from, 'Destinations' => $to_str));
            if ($ses_result) {
                $result->message_id = $ses_result->get('MessageId');
            } else {
                $result->success = false;
                $result->result_text = "Amazon SES did not return a MessageId";
            }
        } catch (Exception $e) {
            $result->success = false;
            $result->result_text = $e->getMessage()." - To: $to_str, Sender: $from, Subject: $subject";
        }
        return $result;
    }

}


class ResultHelper {

    public $success = true;
    public $result_text = "";
    public $message_id = "";

}

?>

I've written a blog post that addresses this, maybe it will be useful to you or others: http://righthandedmonkey.com/2013/06/how-to-use-amazon-ses-to-send-email-php.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...