note 114954 added to function.mail

From: Date: Mon, 05 May 2014 11:29:37 +0000
Subject: note 114954 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-199415@lists.php.net to get a copy of this message
To send mails with attachments in a procedural fashion, you may write a function like this: <?php function mail_with_attachments($to, $subject, $message, Array $filepaths, $from = null, $replyto = null) { $uid = md5(uniqid(time())); $from = str_replace(array("\r", "\n"), '', $from); $headers = (empty($from) ? "" : "From: $from\r\n") . (empty($replyto) ? "" : "Reply-To: $replyto\r\n") . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n" . "This is a multi-part message in MIME format.\r\n" . "--$uid\r\n" . "Content-type: text/plain; charset=iso-8859-1\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . "$message\r\n\r\n"; foreach ($filepaths as $filepath) { if(!file_exists($filepath)) continue; $filename = basename($filepath); $file_contents = file_get_contents($filepath); $file_contents_base64 = base64_encode($file_contents); $content = chunk_split($file_contents_base64); $headers .= "--$uid\r\n" . "Content-Type: application/octet-stream; name=\"$filename\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n" . "$content\r\n\r\n"; } $headers .= "--$uid--"; return mail($to, $subject, "", $headers); } ---- Server IP: 72.52.91.14 Probable Submitter: 202.83.18.30 ---- Manual Page -- http://php.net/manual/en/function.mail.php Edit -- https://master.php.net/note/edit/114954 Del: integrated -- https://master.php.net/note/delete/114954/integrated Del: useless -- https://master.php.net/note/delete/114954/useless Del: bad code -- https://master.php.net/note/delete/114954/bad+code Del: spam -- https://master.php.net/note/delete/114954/spam Del: non-english -- https://master.php.net/note/delete/114954/non-english Del: in docs -- https://master.php.net/note/delete/114954/in+docs Del: other reasons-- https://master.php.net/note/delete/114954 Reject -- https://master.php.net/note/reject/114954 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199415) next »