codeigniter-amazon-ses icon indicating copy to clipboard operation
codeigniter-amazon-ses copied to clipboard

Add attachment support

Open joelcox opened this issue 14 years ago • 8 comments

Amazon just announced support for attachment.

joelcox avatar Jul 19 '11 10:07 joelcox

+1 for this feature

mneumegen avatar Aug 06 '11 03:08 mneumegen

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 avatar Aug 06 '11 06:08 joelcox

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.

joelcox avatar Aug 23 '11 13:08 joelcox

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

fariasweb avatar Nov 27 '12 12:11 fariasweb

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

joelcox avatar Nov 27 '12 18:11 joelcox

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.

fariasweb avatar Nov 28 '12 08:11 fariasweb

Cool. Looking forward to your pull request.

On Nov 28, 2012, at 9:16 AM, Francisco Javier Arias [email protected] 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.

joelcox avatar Nov 28 '12 08:11 joelcox

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;
}

akarupt avatar Sep 05 '23 16:09 akarupt