note 92429 added to function.mail

From: Date: Thu, 23 Jul 2009 19:10:42 +0000
Subject: note 92429 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-158662@lists.php.net to get a copy of this message
Simple function to send mail (text or html) optionally with attachments. <?php function send_mail($to, $from, $subject, $message, $html = false, $attachments = null) { if (empty($to)) return(false); if (empty($from)) return(false); $headers .= "To: $to\r\n"; $headers .= "From: $from\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $xHeader = "Content-type: text/plain; charset=\"iso-8859-1\"\r\n"; if ($html) { // interpret $message as html $xHeader = "Content-type: text/html; charset=\"iso-8859-1\"\r\n"; } if (!is_null($attachments)) { // attachment list delimited w/";" $boundary = md5(mt_rand()); $files = explode(';', $attachments); foreach($files as $file) { $file = trim($file); if (!($data = file_get_contents($file))) continue; // file cannot be opened: skip this attachment $data = chunk_split(base64_encode($data), 68, "\n"); // convert to base64 chunks $contentType = get_mime_type($file); // retreive MIME type from file extension $fname = basename($file); // strip path from filename $xAttach .= "--$boundary\n"; $xAttach .= "Content-Type: $contentType; name=\"$fname\"\n"; $xAttach .= "Content-Transfer-Encoding: base64\n"; $xAttach .= "Content-Disposition: attachment; filename=\"$fname\"\n\n"; $xAttach .= "$data\n"; } if (!empty($xAttach)) { // there is at least one valid attachment $xMessage .= "--$boundary\n"; $xMessage .= $xHeader; // Content-Type: text/plain or text/html depending on $html flag $xMessage .= "Content-Transfer-Encoding: 7bit\n\n"; $xMessage .= "$message\n\n"; $xMessage .= $xAttach; $xMessage .= "--$boundary--\n\n"; $message = $xMessage; $xHeader = "Content-Type: multipart/mixed; boundary=\"$boundary\""; } } $headers .= $xHeader; $status = mail($to, $subject, $message, $headers); return($status); } ?> Notes: [1] Parameters: $html: =true: interpret message as html =false: interpret message as plain text [default] $attachments: list of files to attach, delimited with ";" [2] Returns false if: - $to address not provided - $from address not provided otherwise returns status from mail() function. [3] Any attachments which cannot be opened are skipped. [4] Uses get_mime_type function to set content-type of attachment(s) based on filename extension (see user notes under "mime_content_type"). An alternative is to use mime_content_type function itself, but this fn is not enabled by default in PHP (an issue w/shared hosting). Plus mime_content_type is deprecated. Another options is to use fileinfo functions (see "fileinfo" topic), but these are not enabled either in PHP < 5.3.0. [5] Examples of use: send_mail($to, $from, $subject, $message); // same as using mail. send_mail($to, $from, $subject, $message, TRUE); // interprets $message as html send_mail($to, $from, $subject, $message, FALSE, "../myDir/myAttachment.pdf"); // sends text message with attachment (note relative path) send_mail($to, $from, $subject, $message, TRUE, "att1.txt; att2.pdf; att3.jpeg"); // sends $message as html mail with 3 attachments ---- Server IP: 64.71.164.2 Probable Submitter: 67.100.203.40 ---- Manual Page -- http://www.php.net/manual/en/function.mail.php Edit -- https://master.php.net/note/edit/92429 Del: integrated -- https://master.php.net/note/delete/92429/integrated Del: useless -- https://master.php.net/note/delete/92429/useless Del: bad code -- https://master.php.net/note/delete/92429/bad+code Del: spam -- https://master.php.net/note/delete/92429/spam Del: non-english -- https://master.php.net/note/delete/92429/non-english Del: in docs -- https://master.php.net/note/delete/92429/in+docs Del: other reasons-- https://master.php.net/note/delete/92429 Reject -- https://master.php.net/note/reject/92429 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158662) next »