note 17270 added to function.mail
| From: | chrisg at cordell dot com dot au | Date: | Sun, 02 Dec 2001 22:20:15 +0000 |
| Subject: | note 17270 added to function.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-21541@lists.php.net to get a copy of this message | ||
Attached script adds multiple attachments to a mail after confirming their existence. It checks
their mime type against an array list and sets the 'Content-Type' accordingly.
It would appear that the line $encoded_split = chunk_split($encoded); must be used to send long
attachments, prior to my including this all the attachments on mails I sent were truncated to 814
bytes, which I assume is the maximum line length for some part of the process.
N.B. Code written for windows hence the \r\n at end of lines, you may have to drop the \r from some
places for it to work on other OS's
$mime_list = array(
"html"=>"text/html",
"htm"=>"text/html",
"txt"=>"text/plain",
"rtf"=>"text/enriched",
"csv"=>"text/tab-separated-values",
"css"=>"text/css",
"gif"=>"image/gif",
etc....);
$ABORT = FALSE;
$to = "username@etc.com";
$from = "god";
$subject = "attachment test";
$boundary = "XYZ-" . date(dmyhms) . "-ZYX";
$message = "--$boundary\r\n";
$message .= "Content-Type: text/plain;charset=\"iso-8859-1\"\r\n\r\n";
$message .= "Test Message with attachments\r\n";
//attachment file names with full path
$attachments[1] = "c:\etc";
$attachments[2] = "c:\etc";
$attachments[3] = "c:\etc";
//check that the attachment files exist and if so then encode
foreach ($attachments as $key => $full_path) {
if ($full_path !='') {
if (file_exists($full_path)){
//try to open
if ($fp = fopen($full_path,"rb")) {
//get the file name from the path
$filename = array_pop(explode(chr(92),$full_path));
//get the file extension
$file_extension = substr($filename,strpos($filename,".")+1);
//check to see if the file extension is in the array of standard types else sets the
mime-type to 'text/plain'
in_array($file_extension,array_keys($mime_list))?$mime_type =
$mime_list[$file_extension]:$mime_type = "text/plain";
//read file into $contents
$contents = fread($fp,filesize($full_path));
//encode data
$encoded = base64_encode($contents);
//*****SPLIT THE ENCODED DATA*****
$encoded_split = chunk_split($encoded);
fclose($fp);
$message .= "--$boundary\r\n";
$message .= "Content-Type: $mime_type;name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment\r\n\r\n";
$message .= "$encoded_split\r\n";
}
else {
echo "Cannot open file$key: $filename";
$ABORT = TRUE;
}
}
else {
echo "File$key does not exist: $filename";
$ABORT = TRUE;
}
}
}
$message .= "--$boundary--\r\n";
$headers = "From: <$from>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;boundary=\"$boundary\"\r\n";
if ($ABORT) die;
mail($to,$subject,$message,$headers);
--
http://www.php.net/manual/en/function.mail.php
http://master.php.net/manage/user-notes.php?action=edit+17270
http://master.php.net/manage/user-notes.php?action=delete+17270
http://master.php.net/manage/user-notes.php?action=reject+17270