Posted inProgramming / Mysql / Mysqli / php

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.

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

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype