note 41681 deleted from function.mail by aidan

From: Date: Sun, 08 Aug 2004 15:36:28 +0000
Subject: note 41681 deleted from function.mail by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74237@lists.php.net to get a copy of this message
Note Submitter: j [at] jgdataworks.com ---- I found a simple function to mail MIME e-mail including attachments. I thought I found it in someone's comments on the mail functions, but I can't find it now. The mail_attach function was great, except it created a MIME message whether or not you attached anything. This doesn't look right in Outlook MUA for one, so I modded it to only do MIME if there are attachements. So enough, here it is (thanks to the author of the original): function mail_attach($to, $from, $subject, $message, $files = FALSE,$lb="\n") { // $to Recipient // $from Sender (like "email@domain.com" or "Name <email@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 $mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1)); $header = "From: ".$from; if(is_array($files)) { $header.= $lb; $header.= "MIME-Version: 1.0".$lb; $header.= "Content-Type: multipart/mixed;".$lb; $header.= " boundary=\"".$mime_boundary."\"".$lb; $content = "This is a multi-part message in MIME format.".$lb.$lb; $content.= "--".$mime_boundary.$lb; $content.= "Content-Type: text/plain; charset=\"iso-8859-1\"".$lb; $content.= "Content-Transfer-Encoding: 7bit".$lb.$lb; } $content.= $message.$lb; if(is_array($files)) { $content.= "--".$mime_boundary.$lb; foreach($files as $filename=>$filelocation) { if(is_readable($filelocation)) { $data = chunk_split(base64_encode(implode("", file($filelocation)))); $content.= "Content-Disposition: attachment;".$lb; $content.= "Content-Type: Application/Octet-Stream;"; $content.= " name=\"".$filename."\"".$lb; $content.= "Content-Transfer-Encoding: base64".$lb.$lb; $content.= $data.$lb; $content.= "--".$mime_boundary.$lb; } } } if(mail($to, $subject, $content, $header)) { return TRUE; } return FALSE; }

« previous php.notes (#74237) next »