我创建了一个联系表单,允许上传3个文件。目前,文件大小和文件类型没有限制。 我希望能够做到以下几点:
我目前正在使用PHPMailer通过SMTP发送联系表单。
我的contact_mail.php内容:
<?php
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = FALSE;
$mail->Port = 25;
$mail->Host = "###HIDDEN###";
$mail->Mailer = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("###HIDDEN###");
$mail->Subject = $_POST["subject"];
$mail->WordWrap = 80;
$mail->Body='Name: '.$_POST['userName'].'<br />
Telephone: '.$_POST['telephone'].'<br />
Nationality: '.$_POST['nationality'].'<br />
Age: '.$_POST['age'].'<br />
Height: '.$_POST['height'].'<br/>
Favourites: '.$_POST['age'].'<br />
Bio: '.$_POST['content'];
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile'] ['tmp_name'],$_FILES['attachmentFile']['name']);
$mail->AddAttachment($_FILES['attachmentFile2']['tmp_name'],$_FILES['attachmentFile2']['name']);
$mail->AddAttachment($_FILES['attachmentFile3']['tmp_name'],$_FILES['attachmentFile3']['name']);
}
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
echo "<p class='success'>Contact Mail Sent.</p>";
}
?>
表格内容:
<div class="col-xs-12 col-sm-4 labelPanel">
<label>Portfolio Images</label><br/>
<input type="file" name="attachmentFile" id="attachmentFile" class="inputField">
<input type="file" name="attachmentFile2" id="attachmentFile" class="inputField">
<input type="file" name="attachmentFile3" id="attachmentFile" class="inputField">
</div>
PHP代码&amp;验证:
<script type="text/javascript">
$(document).ready(function (e){
$("#frmContact").on('submit',(function(e){
e.preventDefault();
$('#loader-icon').show();
var valid;
valid = validateContact();
if(valid) {
$.ajax({
url: "contact_mail.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#mail-status").html(data);
$('#loader-icon').hide();
},
error: function(){}
});
}
}));
function validateContact() {
var valid = true;
$(".inputField").css('background-color','');
$(".info").html('');
if(!$("#userName").val()) {
$("#userName-info").html("(required)");
$("#userName").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#telephone").val()) {
$("#telephone-info").html("(required)");
$("#telephone").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#nationality").val()) {
$("#nationality-info").html("(required)");
$("#nationality").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#age").val()) {
$("#age-info").html("(required)");
$("#age").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#height").val()) {
$("#height-info").html("(required)");
$("#height").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#favourites").val()) {
$("#favourites-info").html("(required)");
$("#favourites").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#userEmail").val()) {
$("#userEmail-info").html("(required)");
$("#userEmail").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
$("#userEmail-info").html("(invalid)");
$("#userEmail").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#subject").val()) {
$("#subject-info").html("(required)");
$("#subject").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#content").val()) {
$("#content-info").html("(required)");
$("#content").css('background-color','#FFFFDF');
valid = false;
}
return valid;
}
});
</script>
我对PHP很陌生,所以我按照在线教程创建了联系表单 - 不幸的是,在图片上传验证时,我有点卡住了。