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.