Hi folks,

I see this question asked here all the time, and recently had to implement
it.
Every class and solution I downloaded and tried to use failed for one reason
or
another, so I threw together this, instead.

Advatages:
 * Quick :)
 * It's a function, so you can call it from most places if it's include()'d.

Disadvantages:
 * Quick, so probably buggy :)
 * I haven't put in any facility for sending a HTML version of your mail
text
   yet. We don't send HTML mails @ Melbourne IT, so it wasn't needed.

Usage:

   mailattachments((DestinationAddress), (Subject), (Email Body), (File
Attachment Info),
                   (Extra Headers));

   "File Attachment Info" is an array:

       $fileattach[] = array("filename" =>
"/full/path/to/file/on/your/system",
                             "mimetype" => "mimetype/here");

And the code guts:

<?
   Function mailattachments( $to, $subject, $body, $attacharray,
$extraheaders)
   {
     // Generate a unique boundary
     $mail_boundary = md5(uniqid(time()));
     
     // MIME-compliant headers
     $mailheaders = $extraheaders
                   ."MIME-Version: 1.0\r\n"
                   ."Content-type:
multipart/mixed;boundary=\"$mail_boundary\"\r\n"
                   ."\r\n"
                   ."This is a multipart MIME message\r\n"
                   ."\r\n";
     
     // Body. The part that gets displayed as the message:
     $mailbody = "--$mail_boundary\r\n"
                ."Content-type: text/plain;charset=us-ascii\r\n"
                ."Content-transfer-encoding: 8bit\r\n"
                ."\r\n"
                .$body
                ."\r\n";
     
     // Now, do the attachments
     for ($i = 0; $i < count($attacharray); $i++ )
     {
       $fp = fopen($attacharray[$i][filename], "r");
       $file = fread($fp, filesize($attacharray[$i][filename]));
       
       $file = base64_encode($file);                     // BASE64-encoded.
Text. Nice.
       $file = chunk_split($file);                       // Now in handy
bite-sized 76-char chunks.
       $filename = basename($attacharray[$i][filename]);
       
       $mailbody .= "--$mail_boundary\r\n"
                   ."Content-type: ".$attacharray[$i][mimetype].";
name=".$filename."\r\n"
                   ."Content-transfer-encoding: base64\r\n"
                   ."\r\n"
                   .$file
                   ."\r\n"
                   ."\r\n";
     }
     
     // End of mail
     $mailbody .= "--$mail_boundary--";

     mail($to, $subject, $mailbody, $mailheaders);
   }
?>

Hope this helps someone out there...

Jason

-- 
Jason Murray
[EMAIL PROTECTED]
Web Developer, Melbourne IT
"What'll Scorpy use wormhole technology for?"
'Faster pizza delivery.'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to