如何解决多文件附件和通过表格发送
这是我的两个PHP
文件,用于多个文件附件,但是我无法收到包含多个附件的电子邮件!这是我的两个文件
service-form.PHP
<form name="mailForm" id="mailForm" method="post" action="./email-script/send_email_service_request.PHP" enctype="multipart/form-data">
<input type="hidden" name="subject" value="Request Service or Support">
<div class="w3-container w3-third" style="padding:0px;">
<div class="div-plain quarter-padding">
<div class="input-row">
<label class="form-field-text">Full Name</label>
<span id="userName-info" class="info"></span><br />
<input type="text" class="input-field" name="userName" id="userName" />
</div>
<div class="input-row">
<label class="form-field-text" style="padding-top: 20px;">Company</label>
<span id="Company-info" class="info"></span><br />
<input type="text" class="input-field" name="Company" id="Company" />
</div>
<div class="input-row">
<label class="form-field-text">Email Address</label><br />
<input type="text" class="input-field" name="userEmail" id="userEmail" />
</div>
<div class="input-row">
<label class="form-field-text" style="padding-top: 20px;">Machine Model Number</label>
<span id="ModelNumber-info" class="info"></span><br />
<input type="text" class="input-field" name="ModelNumber" id="ModelNumber" />
</div>
<div class="input-row">
<label class="form-field-text">Machine Serial Number</label><br />
<input type="text" class="input-field" name="SerialNumber" id="SerialNumber" />
<br>(see nameplate on front leg of system)
</div>
</div>
</div>
<div class="w3-container w3-third" style="padding:0px; ">
<div class="div-plain quarter-padding" style="">
<div>
<p class="form-field-sub-text" style="margin-bottom:0px;"> If requesting technical support for an issue,please provide the following information:
</p>
<ul>
<li>
<p class="form-field-sub-text" style="margin-bottom:5px; margin-top:0px;">What was in process
when the trouble started?</p>
</li>
<li>
<p class="form-field-sub-text" style="margin-bottom:5px; margin-top:0px;">Has anything in your
facility or production process changed recently?</p>
</li>
<li>
<p class="form-field-sub-text" style="margin-bottom:5px; margin-top:0px;">What part of the
machine is affected?</p>
</li>
<li>
<p class="form-field-sub-text" style="margin-bottom:5px; margin-top:0px;">Is production stopped
because of this issue?
</p>
</li>
</ul>
<p class="form-field-sub-text" style="margin-bottom:5px; margin-top:0px;"> Otherwise,please provide as
much detail as possible about your situation and service request.</p>
</div>
</div>
</div>
<div class="w3-container w3-third" style="padding:0px; ">
<div class="div-plain quarter-padding" style="">
<div class="input-row">
<label class="form-field-text">Details</label><br><textarea name="Message" id="Message"
class="input-field" id="Message" cols="60" rows="6"></textarea>
</div>
<div class="input-row">
<p class="form-field-text">Supporting Files</p>
<div class="attachment-row">
<input type="file" class="input-field" name="attachment[]" accept=".pdf,.doc,.docx,.xlsx,.xls" />
</div>
<div onClick="addMoreAttachment();" class="icon-add-more-attachemnt" title="Add More Attachments"><img
src="icon-add-more-attachment.png" alt="Add More Attachments">
</div>
</div>
<div>
<input type="submit" name="send" class="btn-submit" value="Send Request" />
</div>
</div>
</div>
<div class="clearfix" style="clear:both"></div>
<div class="mail-sent-status">
<div class="status-message"> </div>
</div>
<div class="overlay"></div>
</form>
<script>
//form validation and submit
$("#mailForm").submit(function(e){
e.preventDefault();
});
$("#mailForm").validate({
rules: {
userName: {
required: true
},Company: {
required: true
},Phone: {
required: true
},ModelNumber: {
required: true
},SerialNumber: {
required: true
},Message: {
required: true
},userEmail: {
required: true,email: true
}
},submitHandler: function(form) {
var msgHolder = $('.mail-sent-status');
var url = $(form).attr('action') ;
var data = $(form).serialize();
var myForm = document.getElementById('mailForm');
var formData = new FormData(myForm);
$.ajax({
url: url,type: 'post',// data: data,data: formData,contentType: false,dataType: 'json',cache: false,processData: false,beforeSend:function () {
$('.overlay').fadeIn();
},success: function(response) {
$('.overlay').fadeOut();
// console.log(response)
var result = response;
// var result = JSON.parse(response);
console.log(result)
if(result.status == false){
msgHolder.find('.status-message').removeClass('success').addClass('Failed').html(result.message)
} else {
$(form).trigger("reset");
msgHolder.find('.status-message').removeClass('Failed').addClass('success').html(result.message)
}
}
});
return false;
}
});
</script>
和
send_email_service_request.PHP
<?PHP
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('error_reporting',E_ALL);
ini_set('display_startup_errors',1);
error_reporting(-1);
//script use from https://www.codexworld.com/PHP-contact-form-send-email-with-attachment-on-submit/
$sub = $_POST["subject"];
$userName = $_POST["userName"];
$company = $_POST["Company"];
$userEmail = $_POST["userEmail"];
$modelNumber = $_POST["ModelNumber"];
$serialNumber = $_POST["SerialNumber"];
$comment = $_POST["Message"];
$uploadedFileType = array();
$uploadedFileName = array();
// Upload attachment file
$uploadStatus = 1;
if(!empty($_FILES["attachment"]["name"])){
// File path config
// $targetDir = dirname(__FILE__)."/email-files/";
// $fileName = basename($_FILES["attachment"]["name"]);
// $targetFilePath = $targetDir . $fileName;
// $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
// // Allow certain file formats
// $allowTypes = array('pdf','doc','docx','xlsx','xls');
// if(in_array($fileType,$allowTypes)){
// // Upload file to the server
// if(move_uploaded_file($_FILES["attachment"]["tmp_name"],$targetFilePath)){
// $uploadedFile[] = $targetFilePath;
// } else {
// $uploadStatus = 0;
// $result['file'] = "Sorry,there was an error uploading your file.";
// }
// } else {
// $uploadStatus = 0;
// $result['file'] = 'Sorry,only PDF,DOC,Excel files are allowed to upload.';
// }
for($i=0; $i < count($_FILES['attachment']['name']); $i++){
$uploadedFileType[] = $_FILES['attachment']['type'][$i];
$uploadedFileName[] = $_FILES['attachment']['name'][$i];
}
}
$files = $uploadedFileName;
$to = "info@online-engineering.com";
// if($uploadStatus == 1){
// Recipient
// $to = "azizulh.ilbd@gmail.com";
// Subject
$subject = "ONLINE Engineering [".$sub."]";
// Message
$htmlContent = '
<html>
<head>
<title>Sender information</title>
</head>
<body>
<table>
<tr>
<th style="text-align:left;width: 160px;">Name: </th>
<td style="text-align:left;">'. $userName .'</td>
</tr>
<tr>
<th style="text-align:left;width: 160px;">Email: </th>
<td style="text-align:left;">'.$userEmail.'</td>
</tr>
<tr>
<th style="text-align:left;width: 160px;">Company: </th>
<td style="text-align:left;">'.$company.'</td>
</tr>
<tr>
<th style="text-align:left;width: 160px;">Machine Model Number: </th>
<td style="text-align:left;">'.$modelNumber .'</td>
</tr>
<tr>
<th style="text-align:left;width: 160px;">Machine Serial Number: </th>
<td style="text-align:left;">'.$serialNumber.'</td>
</tr>
<tr>
<th style="text-align:left;width: 160px;" colspan="2">Details: </th>
</tr>
<tr>
<td style="text-align:left;" colspan="2">'.$comment.'</td>
</tr>
</table>
</body>
</html>
';
// Header for sender info
$headers = "From: $userName"." <".$userEmail.">";
if(!empty($files) && count($files) > 0){
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-transfer-encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
for($x=0;$x<count($files);$x++){
$file = @fopen($files[$x],"rb");
$data = @fread($file,filesize($files[$x]));
@fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-transfer-encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
// $message .= "Content-Type: application/octet-stream; name=\"".$files[$x]."\"\n" .
// "Content-Description: ".$files[$x]."\n" .
// "Content-disposition: attachment;\n" . " filename=\"".$files[$x]."\"; size=".filesize($files[$x]).";\n" .
// "Content-transfer-encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
}
// Send email
$mail = mail($to,$subject,$message,$headers);
} else {
// Set content-type header for sending HTML email
$headers .= "\r\n". "MIME-Version: 1.0";
$headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
// Send email
$mail = mail($to,$htmlContent,$headers);
}
// }
if( $mail ){
$result['status'] = true;
$result['message'] = 'Your mail has been sent successfuly ! Thank you.';
//$result['error'] = $mail->ErrorInfo;
die(json_encode($result));
} else {
$result['status'] = false;
$result['message'] = 'Your mail has not been sent! There is an error. Please try again.';
//$result['error'] = $mail;
die(json_encode($result));
}
//die();
// echo "Mailer Error: " . $mail->ErrorInfo;
?>
这是现场http://online-engineering.com/custom-machining.php
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。