0

I am trying to develop a WordPress plugin which creates a form which first uploads files using Dropzone and then show the form. Once submitted, the form will upload images in stated location and send email to both admin and user. In admin email, it will include the individual links to each file, so when admin click, they can see the image.

So far I have managed almost all of it, but however I am failing to add the links to file in email. Even if I manage I get only file link, whereas I need links for all uploads.

I am using Send grid for the form. The form code is as followed:

<script src="<?php echo plugin_dir_url(__FILE__);?>/dropzone.min.js" type="text/javascript" defer></script>
            <script type="text/javascript">
            var $ = jQuery.noConflict();
            $(function(){
                Dropzone.autoDiscover = false;
                  var dropzone = new Dropzone ("#myAwesomeDropzone", {
                    maxFiles: 3,
                    paramName: "file",
                    url: '<?php echo plugin_dir_url(__FILE__);?>/upload.php',
                    maxFilesize: 1000,
                    previewsContainer: '#dropzonePreview',
                    dictDefaultMessage: "CHOOSE 3 FILES",
                    autoProcessQueue: true,
                    addRemoveLinks: true 
                  });
                  dropzone.on("addedfile", function(file){$('.dz-default').css({ display: "none" });});  
                  dropzone.on("queuecomplete", function (file) {
                        $('.form-show').css({ display: "block" });  
                    $('.form-head').css({ display: "none" });
                    $('.dz-default').css({ display: "none" }); 
                    }); 
                    dropzone.on("maxfilesexceeded", function(file) { this.removeFile(file); }); });
            </script>

            <div class="trial-block">  
            <div>
            <div>
            <form id="myAwesomeDropzone" action="'.plugin_dir_url(__FILE__).'upload.php'.'" class="dropzone" method="post" enctype="multipart/form-data">
            <input type="hidden" name="token" value="'.uniqid().'">
            <input type="hidden" value="" id="Files" name="Files" />
            <ul class="form-show">
            <p class="form-txt">Fill this form.</p>
            <li class="trial-form-item">
            <label class="trial-form-lbl" for="fname">Full Name <span class="trial-must">*</span></label>
            <input type="text" name="fname" id="fname" class="trial-form-input" required>
            </li>
            </li>
            <li class="trial-form-item">
            <label class="trial-form-lbl" for="email">Email <span class="trial-must">*</span></label>
            <input type="email" name="email" id="email" class="trial-form-input" required>
            </li>
            <li class="trial-form-item">
            <label class="trial-form-lbl" for="phone">Telephone <span class="trial-must">*</span></label>
            <input type="tel" name="phone" id="telephone" class="trial-form-input" required>
            </li>
            <div class="clear"></div>
            <li class="trial-form-item trial-form-item-full">
            <label class="trial-form-lbl" for="description">Brief Instruction <span class="trial-must">*</span></label>
            <textarea id="description" name="description" class="trial-form-txtbox trial-form-input" required></textarea>
            </li>
            <li class="trial-form-item trial-form-item-full">
            <div class="dropzone-previews"></div>
            <div class="fallback"><input name="file" type="file" /></div></li>
            <li>
            <input type="submit" id="submit" class="submit-trial" value="Submit">
            </li>
            </ul>
            <div id="dropzonePreview"></div>
            </form>
            </div></div>
            </div>

          // Upload.php

          <?php
          require_once('../../../wp-load.php');
          require 'vendor/autoload.php';

          $sendgrid = new SendGrid('api-code');

          $mail = new SendGrid\Email();// send email to Admin
          $mail2 = new SendGrid\Email();// send confirmation email to client

          // Sender details
          $name = "Example.Com";// Sender name
          $fname = $_POST['fname'];// Sender Name
          $email = $_POST['email'];// Sender Email
          $phone = $_POST['phone'];// Sender Telephone
          $description = $_POST['description'];// Sender Message
          $token = $_POST['token'];//get token to create Order Id

          function cleanNameFunction($name){
              $name = preg_replace("/[^a-zA-Z0-9.]+/", "", $name);
              return $name;
          }

          $target_dir = "wp-content/uploads/trial/";// target direcotry partially added for demo
          $storeFolder = $target_dir .$token.'/';
          $originalName = $_FILES['file']['name'];
          $safeName     = cleanNameFunction($originalName);
          // $target_file = $storeFolder . $safeName ;
          //$tempFile = $_FILES['file']['tmp_name']; 

          $upload_dir = wp_upload_dir();
          $fileurl = $safeName ;  
          // if folder doesn't exists, create it
          if(!file_exists($storeFolder) && !is_dir($storeFolder)) {
              mkdir($storeFolder);
          }

          // if (move_uploaded_file($tempFile, $target_file)) {
          // $status = 1;
          // $fileurl = $safeName ;  

          // }

          //$images_arr = array();
              foreach($_FILES['file']['tmp_name'] as $key=>$val){
                  //upload and stored images
                  $tempFile = $_FILES['file']['tmp_name'][$key];
                  $targetFile =  $storeFolder. $_FILES['file']['name'][$key];
                  move_uploaded_file($tempFile,$targetFile);

                  // $tempFile = $_FILES['file']['tmp_name']; 

                  // $target_file = $storeFolder . $_FILES['file']['name'][$key] ;
                  // move_uploaded_file($_FILES['file']['tmp_name'][$key],$target_file);

                  // if(move_uploaded_file($_FILES['file']['tmp_name'][$key],$target_file)){
                  //  $status = 1;
                  //     $images_arr[] = $target_file;
                  // }
              }

          $msg = "Free Trial Id: $token \r\n\r\nDear Admin,\r\n\r\nA new upload has been submitted.\r\n\r\nName: $fname\r\n\r\nEmail Address: $email\r\n\r\nTelephone: $phone \r\n\r\n\r\nBrief Instruction: $description\r\n\r\n";


            $msg .= "You can download the files from this link: \r\n \n$fileurl";
            $recipient ="example@gmail.com";
            $subject = "$token";
            // Confirmation reciever
            $confirm_subject = "Thank you";
            $confirm_msg = "Dear $fname,\r\n\r\nThank you for trying us. Your free trial Id: $token.";

            // mail($recipient, $subject, $from, $mailheader) or die("Error!");
            $mail->
            addTo( $recipient )->
            setFromName($name)->
            setFrom( $email )->
            setSubject($subject)->
            setText($msg);
            // $mail2: Send Confirmation mail to client
            $mail2->
            addTo( $email )->
            setFromName($name)->
            setFrom( $recipient )->
              setSubject($confirm_subject)->
            setText($confirm_msg);
              //Send Mail.
            if ( ($sendgrid->send($mail)) && ($sendgrid->send($mail2)) ){
              exit(header('Location: example.com'));
            }
            else{
              echo "Order submission failed.";
            }

          ?>

Please have a look and suggest me how can I resolve this. Thank you.

  • Can you please elaborate it bit more? – Davinder Kumar Nov 24 '17 at 06:38
  • as i know, dropzone is for drag and drop to upload. Are you using the same case ? – Davinder Kumar Nov 24 '17 at 06:41
  • You can put the images links in session or cookie in php and while submitting the form, you can include that in mail. Just re-initiate the session on page load. – Davinder Kumar Nov 24 '17 at 06:45
  • yes, I am using drop zone to upload the files. I am not sure to how to do that with PHP session, if you can give me some example it will be great. meanwhile, please have a look in the Upload.php code section, something is wrong there which is causing the problem. however, due to lack of in depth php I am failing to locate the problem. – kamela Chowdhury Nov 24 '17 at 07:09
  • I mean, when you drag and drop image, it uploaded with php, you can save filepath($targetFile ) in session, in array form. All the path will be stored in session as an array, then you simply use it while sending mail. – Davinder Kumar Nov 24 '17 at 07:16
  • Can you please explain little more, may be with an example? – kamela Chowdhury Nov 24 '17 at 07:35
  • Look, 1) User opened the page. 2) He uploads the images, every image goes with ajax request to upload.php so you can store the $targetpath(image path) into the session like $_SESSION['upload_images'][] = $targetpath 3) Suppose user uploads 3 images , then you have all images paths like $_SESSION['upload_images'][0],$_SESSION['upload_images'][1],$_SESSION['upload_images'][2] 4) Now when you are sending the mail, you can include these links easily – Davinder Kumar Nov 24 '17 at 07:45
  • Could you please look into my code and suggest me where should I edit. I am not much used to PHP, hence failing to bind the session with the file path link. a little bit more direction might help me out. thank you. – kamela Chowdhury Nov 24 '17 at 10:19

1 Answers1

0

I was able to get the filename using this following snippet:

dropzone.on("success", function(file,response) {
file.myFileName = file.name;
console.log(file.myFileName);
$("#mDropzone").append($('<input type="hidden" ' +'name="files[]" ' +'value="' + file.myFileName+ '">'));});

The file.myFileName = file.name; gets the file name and append it as input value to the form.