Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** This function can send mail with attachments, custom headers and UTF-8 encoding for body and subject
- * example use: email("david@example.net", "Greeting from holiday", "Hi, here is some my photos. <br><br> See you soon", "john@example.net", "John Example", array("Reply-to" => "helena@example.net"), array("photo1.jpg", "another/photo2.jpg"));
- * @param $to recipient
- * @param $subject subject of the message
- * @param $message text body of the message in HTML format
- * @param $from sender's email
- * @param $from_name [OPTIONAL] sender's name - if not set, then sender's name will be set to sender's email
- * @param $headers [OPTIONAL] array of custom headers in format {"Header-name" => "Header value"}, if necessary
- * @param $files [OPTIONAL] array of files names to attach to message in format {"path/to/file1.bin", "path/to/longer/file2.txt"}
- * @return true if mail was send succesfully
- * @copyright Tomáš Bedřich, ja@tbedrich.cz
- */
- function email($to, $subject, $message, $from, $from_name = "", $headers = array(), $files = array()) {
- // Generate boundary string
- $semi_rand = md5(uniqid(time()));
- $boundary = "--$semi_rand\n";
- // Generate subject
- $subject = "=?utf-8?B?".base64_encode($subject)."?=";
- // Generate custom headers
- $headers = "";
- foreach($headers as $name => $value) {
- $headers .= $name . ": " . $value . "\n";
- }
- // Generate From header
- if(!empty($from_name)) $headers .= "From: =?UTF-8?B?".base64_encode($from_name)."?=<".$from.">\n";
- else $headers .= "From: $from\n";
- // Say, that this will be multipart message
- $headers .= "MIME-Version: 1.0\n";
- $headers .= "Content-Type: multipart/mixed; boundary=\"$semi_rand\"\n\n";
- $headers .= "This is a multi-part message in MIME format.\n";
- $headers .= $boundary;
- // Append text in HTML format
- $headers .= "Content-Type: text/html; charset=utf-8\n";
- $headers .= "Content-Transfer-Encoding: 8bit\n\n";
- $headers .= $message . "\n\n";
- $headers .= $boundary;
- // Append attachments
- for($i = 0; $i < count($files); $i++) {
- // Get name of file and its mime type
- $mime = mime_content_type($files[$i]);
- $basename = strtolower(basename($files[$i]));
- // Read the data and split them into smaller blocks
- $file = fopen($files[$i], "rb");
- $data = fread($file, filesize($files[$i]));
- fclose($file);
- $data = chunk_split(base64_encode($data));
- // Append next part of message which contains file data
- $headers .= "Content-Type: $mime; name=\"$basename\"\n";
- $headers .= "Content-Transfer-Encoding: base64\n";
- $headers .= "Content-Disposition: attachment; filename=\"$basename\"\n\n";
- $headers .= $data . "\n\n";
- // Append boundary except the last file
- if($i != count($files)-1) $headers .= $boundary;
- }
- // Close mail body
- $headers .= "--$semi_rand--";
- // Send mail
- return mail($to, $subject, "", $headers);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement