Skip to main content

Send Multiple attachment with Php mail function

SEND MULTIPLE ATTACHMENT WITH PHP MAIL FUNCTION
step1-create a test2.php 

Demo-Send multiple attachment with PHP MAIL

Demo2-SEND multiple attachment WITH PHP MAIL FUNCTION(AJAX JQUERY)



<center>
<form id="frmEnquiry" action="mail2.php" method="post" enctype='multipart/form-data'>
  <h2>Send Multiple Attachment With Php Mail Function</h2><br>
    <div id="mail-status"></div>
<div id="loader-icon" style="display: none;">
    <img src="LoaderIcon.gif" />
</div>
    <div>
        <input
            type="text" name="userName" id="userName"
            class="demoInputBox" placeholder="Name" required>
    </div>
    <div>
        <input type="text" name="userEmail" id="userEmail"
            class="demoInputBox" placeholder="from email" required>
    </div>
  <div>
        <input type="text" name="toemail" id="toemail"
            class="demoInputBox" placeholder='to email' required>
    </div>
     
   
    <div>
        <textarea name="content" id="content" class="demoInputBox"
            cols="90" rows="26" placeholder="Content"></textarea>
    </div>
    <div>
       <center> <label>Attachment</label><br /> <input type="file"
            name="file[]" class="demoInputBox" multiple="multiple"></div></center>
    <div>
   <center>     <input type="submit" value="Send" class="btnSubmit" />
 <br>
 <button type="reset" class="btnSubmit" value="Reset">Reset</button></center></div>
<div class="wrapperDiv">
  </div>
</form>
</center>

</body>
</html>


Step2-create a mail2.php file
<?php
if($_POST && isset($_FILES['file']))
{
   $recipient_email  = $_POST['toemail'];
   $from_email       = $_POST['userEmail'];
   $subject       = "Attachment email from your website!";
   
   $sender_name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING); //capture sender name
   $sender_email = filter_var($_POST["toemail"], FILTER_SANITIZE_STRING); //capture sender email
   $sender_message = filter_var($_POST["content"], FILTER_SANITIZE_STRING); //capture message
   $attachments = $_FILES['file'];
   
   //php validation
    if(strlen($sender_name)<4){
        die('Name is too short or empty');
    }
   if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
     die('Invalid email');
   }
    if(strlen($sender_message)<4){
        die('Too short message! Please enter something');
    }
   
   $file_count = count($attachments['name']); //count total files attached
   $boundary = md5("sanwebe.com");
         
   if($file_count > 0){ //if attachment exists
      //header
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "From:".$from_email."\r\n";
        $headers .= "Reply-To: ".$sender_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
       
        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= chunk_split(base64_encode($sender_message));
      //attachments
      for ($x = 0; $x < $file_count; $x++){    
         if(!empty($attachments['name'][$x])){
           
            if($attachments['error'][$x]>0) //exit script and output error if we encounter any
            {
               $mymsg = array(
               1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
               2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
               3=>"The uploaded file was only partially uploaded",
               4=>"No file was uploaded",
               6=>"Missing a temporary folder" );
               die($mymsg[$attachments['error'][$x]]);
            }
           
            //get file info
            $file_name = $attachments['name'][$x];
            $file_size = $attachments['size'][$x];
            $file_type = $attachments['type'][$x];
           
            //read file
            $handle = fopen($attachments['tmp_name'][$x], "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
           
            $body .= "--$boundary\r\n";
            $body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
            $body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
            $body .="Content-Transfer-Encoding: base64\r\n";
            $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
            $body .= $encoded_content;
         }
      }
   }else{ //send plain email otherwise
       $headers = "From:".$from_email."\r\n".
        "Reply-To: ".$sender_email. "\n" .
        "X-Mailer: PHP/" . phpversion();
        $body = $sender_message;
   }
     
    $sentMail = @mail($recipient_email, $subject, $body, $headers);
   if($sentMail) //output success or failure messages
   {      
      die('Thank you for your email');
   }else{
      die('Could not send mail! Please check your PHP mail configuration.');  
   }
}
?>

Comments

  1. The Article are written by the author are very informative and have a unique idea to share. Thanks for sharing this Information with us.
    We provide english speaking course in gurgaon at very resonable price with 100% Sucess rate by AIBEDUCATION.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Download Vimeo Thumbnail With PHP

Vimeo Thumbnail Downloader Tool – Vimeo Video Ka Thumbnail Download Karein Aaj ke is tutorial mein hum Vimeo Thumbnail Downloader Tool ke baare mein jaanenge. Is tool ki help se hum Vimeo video URL se us video ki thumbnail image obtain kar sakte hain. Agar aap PHP, Web Development, HTML, CSS aur Online Tools banana seekh rahe hain, to ye ek useful PHP project example hai. Vimeo Thumbnail Downloader Kya Hai? Vimeo Thumbnail Downloader ek simple tool hai jisme user Vimeo video ka URL enter karta hai. Tool Vimeo video ID ko identify karta hai aur available thumbnail image ko display karta hai. Example Vimeo URL: https://vimeo.com/389063954 Is URL mein: 389063954 Vimeo video ID hai. Vimeo Thumbnail Downloader Kaise Work Karta Hai? Is PHP script mein mainly do functions use kiye gaye hain: 1. getVimeoId() Ye function Vimeo URL se video ID find karta hai. 2. getVimeoThumb() Ye function Vimeo video ID ke basis par thumbnail information retrieve karta hai aur available thumbnail...

How To Create Contact Form With Validation-Php & Html

How To Create Contact Form - Hello Friends Ajj Me Apko Btaunga   Ki Kis Prakar   Ham Apni Website Ke Liye Contact Form Bnana Sakta Hai.( By Using   Php Contact Form With Validation Script ) Contact Form Hmare Website Ke Liye Bhut He Jrruri Hota Hai.Contact Form Ke Help Se He User Apna Message Website Owner(Admin) Ko Bhej Sakte Hai. Ajj Me Apko Btane Wala Hu Ki Kis Parkar Ham Contact Form Banakar Apne Website Me Add Kar Sakte Hai Or Apni Website Ke Liye Contact Form Bana Sakte Hai Is Contact Form Me   Ham Php Mail Function() Ka Use Karege . Ab Jab Be Koi User Contact Form Me Apni Information ,Message Details Fill Karke Submit Karega To Uski Sare Information Website Admin Ke Email Id Par A Jaiyegi Jo Be Admin Dwara   Niche Diye Gye Code Me Setup Ki Hogi. Used Language- ·          HTML ·          Jquery ·          Php ...

How To Send Sms From Website Using Php-Textlocal SMS

Hello Friends Ajj Me Apko Btaunga Ki Kis Prakar Aap Apni Website Me Free Sms Send Ka Option Integrate Kar Sakte Ho. Bulk sms in India-Best company Iske Liye Apko Sabse Pehle   Https://Www.Textlocal.In/Signup Par Signup Karke Account Create Karna Hai.Ye Ek Achi bulk sms provider company hai ·          Account Create Hone Ke Bad Aap Apna Username And Password Enter Karke Apne Dashboard Me Login Ho Jaiye( textlocal login) ·          Yanha Apko Text Local Company Dwara 10 Meassge Free Testing Ke Liye Diye Jayege ·          Aap Sms Plan Buy Kar Sakte Ho Is Company Dwara Bhut He Kam Prize Me Sms Pakage,plan provide  Kiye Jate Hai Textlocal Price Plans –  index.php <?php if(isset($_POST['code']) &&  isset($_POST['mobile'])){ // Authorisation details. $username = "enter your textlocal account email id here"; $hash = "...