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 live24u.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!.

You can download source code and Demo from below link.




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.

Also Read :   PHP Dynamic Image Gallery insert update delete With MySQL

<form method="post" action="do_send_attachments.php" enctype="multipart/form-data">
<label>
<span>Sender Name</span>
    <input type="text" value="DSP Patel" name="sender_name" />
</label>
<label>
<span>Sender Email</span>
    <input type="email" value=""  name="sender_email" />
</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-->
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
<!-- OR other ways to used -->
<! -- <input type="file" name="file[]" multiple="multiple" /> -->
</label>
<label>
<input type="submit" value="Send" />
</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    = "live24u@yourmail.com"; 
    $email_form         = "info@live24u.com"; 
    $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)<4){
        die('Too short message! Please enter something');
    }
   
    $file_count = count($data_attech['name']); //count total files attached
    $boundary = md5("sanwebe.com");
           
    if($file_count > 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 < $file_count; $x++){      
            if(!empty($data_attech['name'][$x])){
               
                if($data_attech['error'][$x]>0) //exit script and output error if we encounter any
                {
                    $mymsg = array(
                    1=>"The uploaded file(Live24u.com) exceeds the upload_max_filesize simple directive in php.ini",
                    2=>"The uploaded file(Live24u.com) exceeds the MAX_FILE_SIZE directive simple that was specified in the HTML form",
                    3=>"The uploaded file(Live24u.com) was only simple partially uploaded",
                    4=>"(Live24u.com)No file was simple uploaded",
                    6=>"Missing a simple temporary folder(Live24u.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('Live24u.com - >Thank you for your email');
    }else{
        die('Could not send mail!sorry Please check yourany stuck PHP mail configuration.');  
    }
}
?>

You can download source code and Demo from below link.




Also Read :   Stripe API subscriptions with plan coupon and discounts using PHP

Example

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(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.