PHP SEND mail with Multiple Attachments

PHP SEND mail with Multiple Attachments

In this Post We Will Explain About is PHP SEND mail with Multiple Attachments With Example and Demo.

Welcome on Pakainfo.com – Examples ,The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Sending email with multiple attachments using PHP Script

In this post we will show you Best way to implement Send email with multiple attachments in PHP, hear for How to php scripts: Send an Email with Multiple Attachments in PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

php Form with multiple attachments

index.phpFirst of all we need to simple create HTML multipart/form-data HYML markup form with file Form input fields.

Read Also:  PHP Contact Form Send Email File Attachment Example

<form method="post" action="do_send_attachments.php" enctype="multipart/form-data">
<label>
<span>Sender Name</span>
    
</label>
<label>
<span>Sender Email</span>
    
</label>
<label>
<span>Sender Message</span>
    <textarea name="sender_message"></textarea>
</label>
<label>
<span>Attachments</span>
<!--HTML File input Form fields, we can add as many as required-->
    
    
    
<!-- OR other ways to used -->
<! --  -->
</label>
<label>

</label>
</form>

PHP Mail (do_send_attachments.php)

we can turn those simplefile fields into associative all the arrays using square main brackets as shown source code below, or add simple any file or images attribute multiple=”multiple” in file HTML input tag. this best way no matter how many any more fields we add, we can steps to simply iterate each all the field and access their some php variables later. Have a look simple at HTML form source code below :

<?php
if($_POST && isset($_FILES['file']))
{
    $recipient_email    = "[email protected]"; 
    $email_form         = "[email protected]"; 
    $subject            = "Welcome to love24u.com Attachment email from your website!"; 
   
    $sender_name = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); 
    $email_sender = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); 
    $sender_message = filter_var($_POST["sender_message"], FILTER_SANITIZE_STRING); 
    $data_attech = $_FILES['file'];
   

    if(strlen($sender_name)<4){
        die('Name is too short or empty');
    }
    if (!filter_var($email_sender, FILTER_VALIDATE_EMAIL)) {
      die('Invalid email');
    }
    if(strlen($sender_message) 0){

        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "From:".$email_form."\r\n";
        $headers .= "Reply-To: ".$email_sender."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
       
        $msg_body = "--$boundary\r\n";
        $msg_body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $msg_body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $msg_body .= chunk_split(base64_encode($sender_message));


        for ($x = 0; $x 0) //exit script and output error if we encounter any
                {
                    $mymsg = array(
                    1=>"The uploaded file(Pakainfo.com) exceeds the upload_max_filesize simple directive in php.ini",
                    2=>"The uploaded file(Pakainfo.com) exceeds the MAX_FILE_SIZE directive simple that was specified in the HTML form",
                    3=>"The uploaded file(Pakainfo.com) was only simple partially uploaded",
                    4=>"(Pakainfo.com)No file was simple uploaded",
                    6=>"Missing a simple temporary folder(Pakainfo.com)" );
                    die($mymsg[$data_attech['error'][$x]]);
                }
               
                //get file info
                $file_name = $data_attech['name'][$x];
                $file_size = $data_attech['size'][$x];
                $file_type = $data_attech['type'][$x];
               
                //read file
                $handle = fopen($data_attech['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); 
               
                $msg_body .= "--$boundary\r\n";
                $msg_body .="Content-Type: $file_type; name="$file_name"\r\n";
                $msg_body .="Content-Disposition: attachment; filename="$file_name"\r\n";
                $msg_body .="Content-Transfer-Encoding: base64\r\n";
                $msg_body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
                $msg_body .= $encoded_content;
            }
        }

    }else{ 
       $headers = "From:".$email_form."\r\n".
        "Reply-To: ".$email_sender. "\n" .
        "X-Mailer: PHP/" . phpversion();
        $msg_body = $sender_message;
    }
       
     $sentMail = @mail($recipient_email, $subject, $msg_body, $headers);
    if($sentMail) 
    {      
        die('Pakainfo.com - >Thank you for your email');
    }else{
        die('Could not send mail!sorry Please check yourany stuck PHP mail configuration.');  
    }
}
?>

Example

Read Also:  PHP Mail Attachment - Php Send Email File-Images Attachments

I hope you have Got attachment – How to attach two or multiple files and send mail in PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to Get Yii last inserted ID from database
  2. Read Also:  PHP Send HTML Email File Attachment Using PHPs mail
  3. Read Also:  Rendering Lists in VueJs Example
  4. Read Also:  PHP Filter Multidimensional Array Examples
  5. Read Also:  jQuery Print Dynamic Content using PHP MySQL Example
  6. Read Also:  php send email with attachment example
  7. Read Also:  PHP Multiple Email Attachments with Sending Email AJAX Multiple File upload
  8. Read Also:  Simple Directives using Angular Example
  9. Read Also:  How to force HTTPS or WWW using the htaccess file?
  10. Read Also:  PHP String Cleanup Functions with Example