xml generated when form submitted by user with two attachment in mail in wordpress.

<?php
/*
Plugin Name: Job Invoice
Description: Customer Job Invoice Generate in xml formate
Author: Mahendra Pal
*/

function load_custom_wp_admin_style() {
 wp_register_style( 'customer_invoice_form_css', plugin_dir_url( __FILE__ ) . 'css/invoice_form.css', false, '1.0.0' );
 wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'css/jquery-ui.css', false, '1.0.0' );
 wp_register_script( 'custom_wp_admin_jquery', plugin_dir_url( __FILE__ ) . 'js/jquery-1.12.4.js', false, '1.0.0' );
 wp_register_script( 'custom_wp_admin_jquery_ui', plugin_dir_url( __FILE__ ) . 'js/jquery-ui.js', false, '1.0.0' );
 wp_register_script( 'custom', plugin_dir_url( __FILE__ ) . 'js/custom.js', false, '1.0.0' );
          wp_enqueue_style( 'customer_invoice_form_css' );
          wp_enqueue_style( 'custom_wp_admin_css' );
          wp_enqueue_script( 'custom_wp_admin_jquery' );
          wp_enqueue_script( 'custom_wp_admin_jquery_ui' );
          wp_enqueue_script( 'custom' );
}
add_action( 'wp_enqueue_scripts', 'load_custom_wp_admin_style' );

add_action('admin_menu', 'customer_invoice_pages');
function customer_invoice_pages(){
    add_menu_page('Customer Invoice Page Title', 'Customer Invoice Menu Title', 'manage_options', 'invoice-job', 'customer_invoice_form' );
    add_submenu_page('invoice-job', 'Customer Invoice Page Title', 'Update Customer Invoice Info', 'manage_options', 'invoice-job' );
}

function customer_invoice_form()
{

 if(isset($_POST['submit']))
{
  include_once('../../../wp-config.php');
  include_once('../../../wp-load.php');
  include_once('../../../wp-includes/wp-db.php');
  global $wpdb;
  
   
   $customer = $_POST['customer'];
   $purchase = $_POST['purchase'];
   $color = $_POST['color'];
   $style = $_POST['style'];
   $required_date = $_POST['required_date'];
   $description = $_POST['description'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
   $contact = $_POST['contact'];
  
 $new_image_name=$_FILES["invoice_upload"]["name"];
  
  $target = "upload/";
 
  $allowed =  array('jpg', 'pdf','JPEG');
 
   $ext = pathinfo($new_image_name, PATHINFO_EXTENSION);
 
    if(empty($new_image_name))
       {
             move_uploaded_file($_FILES["invoice_upload"]["tmp_name"], $target. $_FILES["invoice_upload"]["name"]);
              
              $actual_link = "http://$_SERVER[HTTP_HOST]";
              $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
              $Actual_path = dirname($actual_link);
       
         $full_img_path = $Actual_path.'/upload'.'/'.$new_image_name;
       
        $wpdb->insert('wp_customer_invoice', array(
            'customer' => $customer,
            'purchase' => $purchase,
            'color' => $color,
            'style' => $style,
            'req_date' => $required_date,
            'description' => $description,
            'email' => $email,
            'phone' => $phone,
            'contact' => $contact,
            'invoice_upload' => $full_img_path
        ));
        if($wpdb)
        {
            //$message = 'Customer Info Successfully Inserted';
            $doc = new DOMDocument('1.0');
                           header('Content-type: text/xml');
                ob_start();
                  $customer_invoice = "<?xml version='1.0' encoding='UTF-8'?>
                 <Customer_Details>
                <Customer>{$_POST['customer']}</Customer>
                <Purchase>{$_POST['purchase']}</Purchase>
                <Color>{$_POST['color']}</Color>
                <Style>{$_POST['style']}</Style>
                <Date>{$_POST['required_date']}</Date>
                <Description>{$_POST['description']}</Description>
                <Email>{$_POST['email']}</Email>
                <Phone>{$_POST['phone']}</Phone>
                <Contact>{$_POST['contact']}</Contact>
                <Upload>{$_FILES["invoice_upload"]["name"]}</Upload>
                </Customer_Details>";
               
              
               $doc->loadXML($customer_invoice);
              
               $dt = new DateTime();
                          $time = $dt->format('H:i:s');

                          $save_file = $doc->save('upload/'.$time.'.xml');
               
           
                            if($save_file)
                            {
                                // $message = 'xml file saved';
                                $to = "mahendra@obabuji.com";
                                $from = "mymail@mail.com";
                                $subject = "Customer Invoice Subject";
                $message_contant = "Customer Invoice Details Attached";
                //$headers = "From: $from";
               
                // $filename = $_FILES["invoice_upload"]["name"];
                // $content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
               
                $headers = "From: name@domain.com\r\nReply-To: name@domain.com";
               
                $random_hash = md5(date('r', time()));
                $headers .= "\r\nContent-Type: multipart/mixed;
                boundary=\"PHP-mixed-".$random_hash."\"";
                // Set your file path here
               
               
                $invoice_xml =$time.'.xml';
               
                $xml_path = $Actual_path.'/upload'.'/'.$invoice_xml;
               
                $attachment = '';
                $attachment1 = '';$message='';
               
                               for($j=0;$j<2;$j++){
                               if($j==0){
                              
                               $attachment = chunk_split(base64_encode(file_get_contents($xml_path)));
                               }else{
                                $attachment = chunk_split(base64_encode(file_get_contents($full_img_path)));
                               }
               
                //$attachment1 = chunk_split(base64_encode(file_get_contents($full_img_path))); 
               
                //define the body of the message.
                $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
                boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
                $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/html;
                charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
               
               
                //Insert the html message.
                $message .= $message_contant;
                $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
               
               
                //include attachment
            /*    $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/pdf;
                name=\" $new_image_name \"\r\n"."Content-Transfer-Encoding:
                base64\r\n"."Content-Disposition: attachment\r\n\r\n";  */
                if($j==0){
                $message .= "--PHP-mixed-$random_hash\r\n" . "Content-Type: application/zip; name=\"" . $invoice_xml . "\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment\r\n\r\n";
                }else{
                $message .= "--PHP-mixed-$random_hash\r\n" . "Content-Type: application/zip; name=\"" . $new_image_name. "\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment\r\n\r\n";
                }
               
                $message .= $attachment;
                //$message .= $attachment1;
                $message .= "/r/n--PHP-mixed-$random_hash--";
                              
                               }
               
               
               
                $success = @mail($to, $subject, $message, $headers);
                //mail($to,$subject,$message,$headers);
                if ($success) {
                                $message = "<p>mail sent to $to!</p>";
                        } else {
                              $message = "<p>mail could not be sent!</p>";
                        }
                               
                            }
                            else
                            {
                                $message = 'xml file not saved';
                            }
           
           
        }
        else
        {
              $message = 'Customer Info Not Inserted';
        }
       
       
       
       }
       elseif(in_array($ext,$allowed) )
            {
              move_uploaded_file($_FILES["invoice_upload"]["tmp_name"], $target. $_FILES["invoice_upload"]["name"]);
             
              $actual_link = "http://$_SERVER[HTTP_HOST]";
              $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
              $Actual_path = dirname($actual_link);
       
         $full_img_path = $Actual_path.'/upload'.'/'.$new_image_name;
       
       
        $wpdb->insert('wp_customer_invoice', array(
            'customer' => $customer,
            'purchase' => $purchase,
            'color' => $color,
            'style' => $style,
            'req_date' => $required_date,
            'description' => $description,
            'email' => $email,
            'phone' => $phone,
            'contact' => $contact,
            'invoice_upload' => $full_img_path
        ));
        if($wpdb)
        {
            //$message = 'Customer Info Successfully Inserted';
           
            $doc = new DOMDocument('1.0');
             header('Content-type: text/xml');
                ob_start();
                    $customer_invoice = "<?xml version='1.0' encoding='UTF-8'?>
                 <Customer_Details>
                <Customer>{$_POST['customer']}</Customer>
                <Purchase>{$_POST['purchase']}</Purchase>
                <Color>{$_POST['color']}</Color>
                <Style>{$_POST['style']}</Style>
                <Date>{$_POST['required_date']}</Date>
                <Description>{$_POST['description']}</Description>
                <Email>{$_POST['email']}</Email>
                <Phone>{$_POST['phone']}</Phone>
                <Contact>{$_POST['contact']}</Contact>
                <Upload>{$_FILES["invoice_upload"]["name"]}</Upload>
                </Customer_Details>";
               
                $doc->loadXML($customer_invoice);
              
               $dt = new DateTime();
                          $time = $dt->format('H:i:s');

                          $save_file = $doc->save('upload/'.$time.'.xml');
               
           
                            if($save_file)
                            {
                                // $message = 'xml file saved';
                                $to = "mahendra@obabuji.com";
                                $from = "mymail@mail.com";
                                $subject = "Customer Invoice Subject";
                $message_contant = "Customer Invoice Details Attached";
                //$headers = "From: $from";
               
                // $filename = $_FILES["invoice_upload"]["name"];
                // $content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
               
                $headers = "From: name@domain.com\r\nReply-To: name@domain.com";
               
                $random_hash = md5(date('r', time()));
                $headers .= "\r\nContent-Type: multipart/mixed;
                boundary=\"PHP-mixed-".$random_hash."\"";
                // Set your file path here
               
               
                $invoice_xml =$time.'.xml';
               
                $xml_path = $Actual_path.'/upload'.'/'.$invoice_xml;
               
                $attachment = '';
                $attachment1 = '';$message="";
               
                               for($j=0;$j<2;$j++){
                               if($j==0){
                              
                               $attachment = chunk_split(base64_encode(file_get_contents($xml_path)));
                               }else{
                                $attachment = chunk_split(base64_encode(file_get_contents($full_img_path)));
                               }
               
                //$attachment1 = chunk_split(base64_encode(file_get_contents($full_img_path))); 
               
                //define the body of the message.
                $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
                boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
                $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/html;
                charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
               
               
                //Insert the html message.
                $message .= $message_contant;
                $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
               
               
                //include attachment
            /*    $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/pdf;
                name=\" $new_image_name \"\r\n"."Content-Transfer-Encoding:
                base64\r\n"."Content-Disposition: attachment\r\n\r\n";  */
                if($j==0){
                $message .= "--PHP-mixed-$random_hash\r\n" . "Content-Type: application/zip; name=\"" . $invoice_xml . "\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment\r\n\r\n";
                }else{
                $message .= "--PHP-mixed-$random_hash\r\n" . "Content-Type: application/zip; name=\"" . $new_image_name. "\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment\r\n\r\n";
                }
               
                $message .= $attachment;
                //$message .= $attachment1;
                $message .= "/r/n--PHP-mixed-$random_hash--";
                              
                               }
               
               
               
                $success = @mail($to, $subject, $message, $headers);
                //mail($to,$subject,$message,$headers);
                if ($success) {
                                $message = "<p>mail sent to $to!</p>";
                        } else {
                              $message = "<p>mail could not be sent!</p>";
                        }
                            }
                            else
                            {
                                $message = 'xml file not saved';
                            }
        }
        else
        {
              $message = 'Customer Info Not Inserted';
        }
       
       
            }
            else
            {
               $message='You must upload a file with one of the following extensions JPG, PDF';
              
            }
  
  
  
  
}
?>
  <form action="" name="customer_invoice" method="post" enctype="multipart/form-data" style="border:1px solid #ccc">
  <div class="message"><?php if($message!="") { echo $message; } ?></div> 
  <div class="container">
    <label><b>Customer:</b></label>
    <input type="text" placeholder="Customer" name="customer" />
   
      <label><b>Purchase:</b></label>
    <input type="text" placeholder="Purchase" name="purchase" />
   
     <label><b>Color:</b></label>
    <select name="color">
    <option>white</option>
     <option>green</option>
      <option>yellow</option>
       <option>black</option>
    </select>
   
    <label><b>Style:</b></label>
    <select name="style">
    <option>x1</option>
     <option>x2</option>
      <option>x3</option>
       <option>x4</option>
    </select>
   <label><b>Required Date:</b></label>
   <input type="text" id="required_date" name="required_date" class="field-style field-split align-right" placeholder="Required DATE" />
  
   <label><b>Description:</b></label>
   <textarea rows="4" cols="50" id="description" name="description" class="field-style field-split align-right" placeholder="Description">
   </textarea>
  
       <label><b>Email:</b></label>
    <input type="text" placeholder="Email" name="email" />
   
    <label><b>Phone:</b></label>
    <input type="text" placeholder="Phone" name="phone" />
   
     <label><b>Contact:</b></label>
    <input type="text" placeholder="Contact" name="contact" />
   
     <label><b>Please Upload:</b></label>
     <input type="file" name="invoice_upload" id="invoice_upload">
  
   <div class="clearfix">
    <input type="submit" name="submit" class="signupbtn" value="Submit" />
    </div>
  </div>
</form>
<?php 
}

add_shortcode( 'invoice_form', 'customer_invoice_form' );

Comments

Popular posts from this blog

dynamic create multiple ckeditor onclick event using jquery

get lat and long from city name using jquery

membership registration and login system in php