mistergonza
5/26/2015 - 9:34 AM

Simple mail function supports attachments

Simple mail function supports attachments

<?php
function send_mail($to, $from, $subj, $text, $files = null)
{
   $subj='=?windows-1251?B?'.base64_encode($subj).'?=';
   $boundary = "--".strtoupper(md5(uniqid(rand())));
   $headers  = "From: ".strtoupper($_SERVER['SERVER_NAME'])." <".$from.">\r\n";  
   $headers .= "Return-path: <".$from.">\r\n";
   $headers .= "MIME-Version: 1.0\r\n";
   $headers .="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
   
      $type  = 'text/html';
      $body  =  "--".$boundary."\r\n";
      $body .= "Content-Type: ".$type."; charset=windows-1251\r\n";
      $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
      $body .= $text."\r\n";
              
   if ((is_array($files)) && (!empty($files)))
   {
       foreach($files as $filename => $filecontent)
       {
          $body .= "--".$boundary."\r\n";
          $body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
          $body .= "Content-Transfer-Encoding: base64\r\n";
          $body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
          $body .= chunk_split(base64_encode($filecontent))."\r\n";
       }
       $body .= "--".$boundary."--";
   }
   return mail($to, $subj, $body, $headers);
}
?>