我正在尝试开发一个WordPress插件,该插件创建一个表单,首先使用Dropzone上传文件,然后显示表单。提交后,表单将在指定位置上传图像并向管理员和用户发送电子邮件。在管理员电子邮件中,它将包含指向每个文件的各个链接,因此当管理员点击时,他们可以看到图像。
到目前为止,我已经管理了几乎所有这些,但是我没有在电子邮件中添加链接到文件。即使我管理我只获得文件链接,而我需要所有上传的链接。
我正在使用表单的发送网格。表单代码如下:
<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.";
}
?>
请看一下并建议我如何解决这个问题。谢谢。