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 – 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 :

if($_POST && isset($_FILES['file']))
    $recipient_email    = "[email protected]"; 
    $email_form         = "[email protected]"; 
    $subject            = "Welcome to 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'];

        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( exceeds the upload_max_filesize simple directive in php.ini",
                    2=>"The uploaded file( exceeds the MAX_FILE_SIZE directive simple that was specified in the HTML form",
                    3=>"The uploaded file( was only simple partially uploaded",
                    4=>"( file was simple uploaded",
                    6=>"Missing a simple temporary folder(" );
                //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);
                $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;

       $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);
        die(' - >Thank you for your email');
        die('Could not send mail!sorry Please check yourany stuck PHP mail configuration.');  


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

Leave a Comment