asachanfbd

PHP Mail with attachments

Feb 17th, 2013
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.    function attachfile($filename, $filedata, $return = false, $reset = false){
  3.         static $attachment = '';
  4.         if($return){
  5.             return $attachment;
  6.         }
  7.         if($reset){
  8.             $attachment = '';
  9.         }
  10.         if($filename != '' && $filedata != ''){
  11.             $filedata = chunk_split(base64_encode($filedata));
  12.             $attachment .= "--{".mime_boundary()."}\n".
  13.             "Content-Type: application/octet-stream; name=\"".($filename)."\"\n".
  14.             "Content-Transfer-Encoding: base64\n".
  15.             "Content-Disposition: attachment; filename=\"".($filename)."\"\n\n".$filedata."\n\n";
  16.         }
  17.     }
  18.    
  19.    function mime_boundary(){
  20.         static $mb = '';
  21.         if($mb == ''){
  22.             $mb = "==Multipart_Boundary_x".md5(uniqid(time()))."x";
  23.         }
  24.         return $mb;
  25.     }
  26.    
  27.     /**
  28.     * before calling this function call attachfile function with required inputs and then call send mail the mail will be sent with attachments.
  29.     *
  30.     * @param mixed $to
  31.     * @param mixed $subject
  32.     * @param mixed $msg
  33.     * @param mixed $sender
  34.     * @param mixed $sendername
  35.     * @return mixed
  36.     */
  37.    
  38.     function sendmail($to, $subject, $msg, $sender = '[email protected]', $sendername = 'Sample Sender Name'){
  39.             global $db;
  40.             $from = $sendername." <".$sender.">";
  41.             $headers = "From: ".$from;
  42.             $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{".mime_boundary()."}\"";
  43.             $message = "--{".mime_boundary()."}\n";
  44.             $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";
  45.             $message .= '<html><body>'.$msg."</body></html>\n\n";
  46.            
  47.             $message .= attachfile('', '', true);
  48.             $message .= "--{".mime_boundary()."}--";
  49.             $returnpath = "-f".$sender;
  50.            
  51.             if(mail($to, $subject, $message, $headers, $returnpath)){
  52.                 return 'Mail Sent';
  53.             }else{
  54.                 return 'Mail not accepted by Server';
  55.             }
  56.     }
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment