Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Listing 18.6 Sending Attachments


<?
/*
Function: makeAttachment
Input: ARRAY attachment
Output: STRING
Description: Returns headers and data for one
attachment. It expects an array with elements
type, name, and content. Attachments are naively
* base64 encoded, even when unnecessary.
/
function makeAttachment($attachment)
{
//send content type
$headers = "Content-Type: ". $attachment["type"];


if($attachment["name"] != "")
{
$headers .= "; name=
\"{$attachment["name"]}\"";
}


$headers .= "\r\n";


$headers .= "Content-Transfer-Encoding:
base64\r\n";


$headers .= "\r\n";


$headers .=
chunk_split(base64_encode($attachment["content"]));
$headers .= "\r\n";


return($headers);
}


/*
Function: mailAttachment
Input: STRING to, STRING from, STRING subject, ARRAY
attachment
Output: none
Description: Sends attachments via email. The
attachment
array is a 2D array. Each element is an associative
array
containing elements type, name and content.

Free download pdf