Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attachment support #7

Open
joelcox opened this issue Jul 19, 2011 · 8 comments
Open

Add attachment support #7

joelcox opened this issue Jul 19, 2011 · 8 comments

Comments

@joelcox
Copy link
Owner

joelcox commented Jul 19, 2011

Amazon just announced support for attachment.

@mneumegen
Copy link

+1 for this feature

@joelcox
Copy link
Owner Author

joelcox commented Aug 6, 2011

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
Copy link
Owner Author

joelcox commented Aug 23, 2011

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
Copy link

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

@joelcox
Copy link
Owner Author

joelcox commented Nov 27, 2012

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

@fariasweb
Copy link

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
Copy link
Owner Author

joelcox commented Nov 28, 2012

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
Copy link

akarupt commented Sep 5, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants