NYCPHP Meetup

NYPHP.org

[nycphp-talk] custom mail() with attachment problem using RH7.3/Apache

Phillip Powell phillip.powell at adnet-sys.com
Thu Jun 10 17:30:59 EDT 2004


I wrote a custom mail() function that should handle emailing with 
attachments, based upon content I saw in the PHP manual and on 
www.phpbuilder.com:

[PHP]
// ORIGINAL FUNCTION FOUND AT 
http://www.php.net/manual/en/function.mail.php#41681 WITH cc AND bcc 
MODIFICATIONS BY PHIL POWELL 6/10/2004
function mail_attach($to, $subject, $message, $from, $files = false, 
$lb="\n", $cc = '', $bcc = '') {
 /*----------------------------------------------------------------------------------------------
     $to Recipient
     $from Sender (like "email at domain.com" or "Name <email at domain.com>")
     $subject Subject
     $message Content
     $files hash-array of files to attach
     $lb is linebreak characters... some mailers need \r\n, others need \n
-------------------------------------------------------------------------------------------------*/
 global $phpVersionInt;
 $mime_boundary = '==Multipart_Boundary_x' . md5(uniqid(mt_rand(), 1)) . 
'x';
 $header = 'From: '. $from;
 if ($cc) $header .= $lb . "Cc: $cc";
 if ($bcc && (int)$phpVersionInt > 430) $header .= $lb . "Bcc: $bcc";

 // Add the headers for a file attachment
 $headers .= "\nMIME-Version: 1.0\n" .
                      "Content-Type: multipart/mixed;\n" .
                      " boundary=\"{$mime_boundary}\"";

 if (is_array($files)) {
  $header.= $lb .
                    "\nMIME-Version: 1.0$lb" .
                    "\nContent-Type: multipart/mixed;\n" .
                    " boundary=\"$mime_boundary\"\n";
  $content = "This is a multi-part message in MIME format.\n\n" .
                      "--$mime_boundary\n" .
                      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                      "Content-Transfer-Encoding: 7bit\n\n";
 }
 $content .= "$message\n";

 if (is_array($files)) {
  $content.= "--{$mime_boundary}\n";
  foreach ($files as $filename => $filelocation) {
   if (is_readable($filelocation) && is_file($filelocation)) {
    $fileID = @fopen($filelocation);
    $data = @fread($fileID, filesize($filelocation));
    @fclose($fileID);
    $data = chunk_split(base64_encode($data));
    // Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
                         'Content-Type: ' . 
mime_content_type($filelocation) . ";\n" .
                         " name=\"{$filename}\"\n" .
                         "Content-Disposition: attachment;\n" .
                         " filename=\"{$filename}\"\n" .
                         "Content-Transfer-Encoding: base64\n\n" .
                         $data . "\n\n" .
                         "--{$mime_boundary}--\n";
   }
  }
 }
 if (mail($to, $subject, $content, $header)) return true;
 return false;
}
[/PHP]

However, upon utilizing this function, the site that calls it crashes, 
mail is never delivered and resources are locked for a good 5 minutes or 
more!

I checked my mail settings in php.ini and they seem utterly standard:

sendmail_path = /usr/sbin/sendmail -t -i

I am figuring that it might be in the way I'm writing the headers but 
can't verify that either.  Ideas welcomed, I can't borrow however as 
this is governmental and by regulation everything has to be 100% 
custom.  Go figure.

Thanx
Phil

-- 
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107 
Fax: (703) 709-7219

	




More information about the talk mailing list