joelcox / codeigniter-amazon-ses

A CodeIgniter library to interact with Amazon Web Services (AWS) Simple Email Service (SES)
http://joelcox.nl
MIT License
79 stars 31 forks source link

Add attachment support #7

Open joelcox opened 13 years ago

joelcox commented 13 years ago

Amazon just announced support for attachment.

mneumegen commented 13 years ago

+1 for this feature

joelcox commented 13 years ago

I probably won't come around to do this in the next two weeks due to my holiday, so don't hold your breath ;-).

joelcox commented 13 years ago

I've read through the new API docs and it seems that adding attachments requires you to use the SendRawEmail endpoint instead of the (easier) SendEmail endpoint this library is currently using. Switching would require quite some rewriting, since this resources requires you to write all the headers yourself, but allows for more features in the future (like DKIM signing). Watch this space.

fariasweb commented 11 years ago

I developed this feature the last week in the job, i will create a branch to upload it and share it

joelcox commented 11 years ago

Sounds great Francisco. Using the SendRawEmail endpoint, I presume?

fariasweb commented 11 years ago

Yes, I will use a SendRawEmail endpoint if you send and email with attachment but if you dont have attachment in the email i will use a SendEmail endpoint.

joelcox commented 11 years ago

Cool. Looking forward to your pull request.

On Nov 28, 2012, at 9:16 AM, Francisco Javier Arias notifications@github.com wrote:

Yes, I will use a SendRawEmail endpoint if you send and email with attachment but if you dont have attachment in the email i will use a SendEmail endpoint.

— Reply to this email directly or view it on GitHub.

akarupt commented 1 year ago

For it works i needed to change the _format_query_raw_string to this.

private function _format_query_raw_string() { $query_string = array( 'Action' => 'SendRawEmail', 'Source' => ($this->from_name ? $this->from_name . ' <' . $this->from . '>' : $this->from), 'RawMessage.Data' => "" );

    $boundary = uniqid(rand(), true);

    // Add all recipients to array
    if (isset($this->recipients['to']))
    {

        for ($i = 0; $i < count($this->recipients['to']); $i++)
        {
            $query_string['Destinations.member.' . ($i + 1)] = $this->recipients['to'][$i];  
        }   

        $query_string['RawMessage.Data'] .= "To:".implode(",", $this->recipients['to'])."\n";
    }

    $query_string['RawMessage.Data'] .= "Subject: ".$this->subject."\n";

    if (isset($this->recipients['cc']) and count($this->recipients['cc']))
    {
        $query_string['RawMessage.Data'] .= "Cc:".implode(",", $this->recipients['cc'])."\n";
    }

    if (isset($this->recipients['bcc']))
    {
        $query_string['RawMessage.Data'] .= "Bcc:".implode(",", $this->recipients['bcc'])."\n";
    }

    if (isset($this->reply_to) AND ( ! empty($this->reply_to))) 
    {
        $query_string['RawMessage.Data'] .= "Reply-To:".$this->reply_to."\n";
    }

    $query_string['RawMessage.Data'] .= "MIME-Version: 1.0\n";
    $query_string['RawMessage.Data'] .= "Content-Type: Multipart/Mixed; boundary=\"".$boundary."\"\n";

    $query_string['RawMessage.Data'] .= "\n--".$boundary."\n";
    $query_string['RawMessage.Data'] .= 'Content-Type: Multipart/Alternative; boundary="alt-' . $boundary . '"' . "";

    /*$query_string['RawMessage.Data'] .= "\n\n--alt-".$boundary."\n";
    $query_string['RawMessage.Data'] .= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $query_string['RawMessage.Data'] .= "Content-Transfer-Encoding: quoted-printable\n\n";

    $query_string['RawMessage.Data'] .= $this->_prep_quoted_printable($this->message);*/

    $query_string['RawMessage.Data'] .= "\n\n--alt-".$boundary."\n";
    $query_string['RawMessage.Data'] .= "Content-Type: text/plain; charset=\"UTF-8\"\n\n";

    $query_string['RawMessage.Data'] .= (empty($this->message_alt) ? strip_tags($this->message) : $this->message_alt);

    $query_string['RawMessage.Data'] .= "\n\n--alt-".$boundary."\n";
    $query_string['RawMessage.Data'] .= "Content-Type: text/html; charset=\"UTF-8\"\n\n";

    $query_string['RawMessage.Data'] .= $this->message;

    $query_string['RawMessage.Data'] .=  "\n\n--alt-".$boundary."--\n";

    //Add attachment
    foreach($this->attach as $a){
        $query_string['RawMessage.Data'] .= "\n--".$boundary."\n";
        $query_string['RawMessage.Data'] .= "Content-Type: ".$a['mime_type']."; name=\"".$this->_prep_quoted_printable($a['name']).".".$a['ext']."\"\n";
        $query_string['RawMessage.Data'] .= "Content-Description: \"".$this->_prep_quoted_printable($a['name'])."\"\n";
        $query_string['RawMessage.Data'] .= "Content-Disposition: attachment; filename=\"".$this->_prep_quoted_printable($a['name']).".".$a['ext']."\"; size=".filesize($a['filename']).";\n";
        $query_string['RawMessage.Data'] .= "Content-Transfer-Encoding: base64\n\n";

        $filetype = pathinfo($a['filename'], PATHINFO_EXTENSION);
        $imgbinary = fread(fopen($a['filename'], "r"), filesize($a['filename']));

        $query_string['RawMessage.Data'] .= chunk_split(base64_encode($imgbinary), 76, "\n") . "\n";
    }

    $query_string['RawMessage.Data'] .= "--".$boundary."--\n";

    $query_string['RawMessage.Data'] = base64_encode($query_string['RawMessage.Data']);

    return $query_string;
}