Advertisement
Guest User

UTF-8 Mail with attachments and custom headers

a guest
Aug 29th, 2010
452
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** This function can send mail with attachments, custom headers and UTF-8 encoding for body and subject
  2.     * 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"));
  3.     *    @param $to recipient
  4.     *    @param $subject subject of the message
  5.     *    @param $message text body of the message in HTML format
  6.     *    @param $from sender's email
  7.     *    @param $from_name [OPTIONAL] sender's name - if not set, then sender's name will be set to sender's email
  8.     *    @param $headers [OPTIONAL] array of custom headers in format {"Header-name" => "Header value"}, if necessary
  9.     *    @param $files [OPTIONAL] array of files names to attach to message in format {"path/to/file1.bin", "path/to/longer/file2.txt"}
  10.     *    @return true if mail was send succesfully
  11.     *    @copyright Tomáš Bedřich, ja@tbedrich.cz
  12.     */
  13.  
  14. function email($to, $subject, $message, $from, $from_name = "", $headers = array(), $files = array()) {
  15.    
  16.     // Generate boundary string
  17.     $semi_rand = md5(uniqid(time()));
  18.     $boundary = "--$semi_rand\n";
  19.    
  20.     // Generate subject
  21.     $subject = "=?utf-8?B?".base64_encode($subject)."?=";
  22.    
  23.     // Generate custom headers
  24.     $headers = "";
  25.     foreach($headers as $name => $value) {
  26.         $headers .= $name . ": " . $value . "\n";
  27.     }
  28.    
  29.     // Generate From header
  30.     if(!empty($from_name)) $headers .= "From: =?UTF-8?B?".base64_encode($from_name)."?=<".$from.">\n";
  31.     else $headers .= "From: $from\n";
  32.    
  33.     // Say, that this will be multipart message
  34.     $headers .= "MIME-Version: 1.0\n";
  35.     $headers .= "Content-Type: multipart/mixed; boundary=\"$semi_rand\"\n\n";
  36.     $headers .= "This is a multi-part message in MIME format.\n";
  37.     $headers .= $boundary;
  38.    
  39.     // Append text in HTML format
  40.     $headers .= "Content-Type: text/html; charset=utf-8\n";
  41.     $headers .= "Content-Transfer-Encoding: 8bit\n\n";
  42.     $headers .= $message . "\n\n";
  43.     $headers .= $boundary;
  44.    
  45.     // Append attachments
  46.     for($i = 0; $i < count($files); $i++) {
  47.        
  48.         // Get name of file and its mime type
  49.         $mime = mime_content_type($files[$i]);
  50.         $basename = strtolower(basename($files[$i]));
  51.  
  52.         // Read the data and split them into smaller blocks
  53.         $file = fopen($files[$i], "rb");
  54.         $data = fread($file, filesize($files[$i]));
  55.         fclose($file);
  56.         $data = chunk_split(base64_encode($data));
  57.        
  58.         // Append next part of message which contains file data
  59.         $headers .= "Content-Type: $mime; name=\"$basename\"\n";
  60.         $headers .= "Content-Transfer-Encoding: base64\n";
  61.         $headers .= "Content-Disposition: attachment; filename=\"$basename\"\n\n";
  62.         $headers .= $data . "\n\n";
  63.        
  64.         // Append boundary except the last file
  65.         if($i != count($files)-1) $headers .= $boundary;
  66.     }
  67.    
  68.     // Close mail body
  69.     $headers .= "--$semi_rand--";
  70.    
  71.     // Send mail
  72.     return mail($to, $subject, "", $headers);
  73. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement