Friday, 6 July 2012

Attach multiple files in the mail using php


 The following function will send a mail with attachment.. Just need to copy-paste the function and made basic changes in it.

 /**
 *  parameter :  Array $attach Contains the number of file and their names
 *  parameter : String $message   Message to the user like PFA
 *  parameter : $mailto receiver@abc.com
 *  parameter : $subject This is attached mail.
**/             

 function   mail_with_attachment($attach,$message, $mailto, $subject )
{
       
                 $files = array();
                $unique_id = md5(time());
                foreach($attach as $a){
                  
               $attachment = './path/to/file/folder/'.$a['filename'];                  
               $aFile = fopen($attachment,"rb");
               $data = fread($aFile,filesize($attachment));
               fclose($aFile);
               $data = chunk_split(base64_encode($data));
               $headers .= "MIME-Version: 1.0\r\n";
               $headers .= "Content-Type: multipart/mixed; boundary=\"".$unique_id."\"\r\n\r\n";
               $headers .= "--".$unique_id."\r\n";
               $headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
              $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
              $headers .= $message."\r\n\r\n";
              $headers .= "--".$unique_id."\r\n";
              $headers .= "Content-Type: application/octet-stream; name=\"".$a['filename']."\"\r\n";
              $headers .= "Content-Transfer-Encoding: base64\r\n";
              $headers .= "Content-Disposition: attachment; filename=\"".$a['filename']."\"\r\n\r\n";
              $headers .= $data."\r\n\r\n";
              $headers .= "--".$unique_id."--";
   
               
              //MAIL TO USER
             $mailSend = mail ($mailto , $subject ," " , $headers); 
         if($mailSend)
        {
            echo "Mail Send Successfully .. :-)";
         }
        else
        {
             echo "Sorry, there is some error.. Comment to me ... :-( ";
        }


}//End Function
             

No comments:

Post a Comment