note 41681 added to function.mail

From: Date: Mon, 19 Apr 2004 18:18:28 +0000
Subject: note 41681 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-68337@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/function.mail.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41681 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41681&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41681&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68337) next »