我跟著這個(gè)教程:https://www.youtube.com/watch? v = DKq1n-awl CW 如何在我的網(wǎng)站實(shí)現(xiàn)一個(gè)工作聯(lián)系形式?
一切都很順利,直到我發(fā)送了第一封測(cè)試郵件,發(fā)現(xiàn)郵件確實(shí)發(fā)送了,但是沒有任何信息:收到了空郵件。
我不是jquery或php方面的專家,但我知道它是如何工作的,而且我似乎找不到為什么它不能像預(yù)期的那樣工作。
我試著到處尋找一些變量,但沒有任何結(jié)果。
以下是HTML:
<form id="contactform" class="form-element" action="php/contact.php" method="post">
<label class="form-field">
<span>Name</span>
<input id="name" class="form-component" type="text" name="name" autocomplete="off" required>
</label>
<label class="form-field">
<span>Email</span>
<input id="email" class="form-component" type="email" name="email" autocomplete="off" required>
</label>
<label class="form-field">
<span>Subject</span>
<input id="subject" class="form-component" type="text" name="subjects" autocomplete="off" required>
</label>
<label class="form-field">
<span>Message</span>
<textarea id="message" class="form-text-area" name="message" autocomplete="off" required></textarea>
</label>
<div class="form-field">
<button class="form-submit" type="button" onclick="sendEmail()" value="Send an email.">Submit</button>
</div>
</form>
這里是腳本:
<script type="text/javascript">
function sendEmail(){
var name = $("#name");
var email = $("#email");
var subject = $("#subject");
var message = $("#message");
if(isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(message)){
$.ajax({
url: 'php/contact.php',
method: 'POST',
dataType: 'json',
data:{
name: name.val(),
email: email.val(),
subject: subject.val(),
message: message.val()
}, success: function(response){
$('#contactform')[0].reset();
$('.confirm-title').text("Message sent successfully!");
}
});
}
}
function isNotEmpty(caller){
if(caller.val()=="") {
caller.css('border', '1px solid red');
return false;
}
else {
caller.css('border', '');
return true;
}
}
</script>
最后,這是PHP文檔:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $POST['name'];
$email = $POST['email'];
$subject = $POST['subject'];
$message = $POST['message'];
require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/Exception.php";
require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/PHPMailer.php";
require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/SMTP.php";
$mail = new PHPMailer();
// SMTP Settings
$mail->isSMTP();
$mail->Host = "secret";
$mail->SMTPAuth = true;
$mail->Username = 'secret';
$mail->Password = 'secret';
$mail->Port = secret;
$mail->SMTPSecure = "ssl";
// Email settings
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("secret");
$mail->Subject = ("$email ($subject)");
$MailBody = "From: ".$name."<br>";
$MailBody .= "Email: ".$email."<br>";
$MailBody .= "Message: ".$message."<br>";
$mail->Body = $MailBody;
if($mail->send()){
$status = "success";
$response = "Email is sent !";
}
else {
$status = "failed";
$response = "Something is wrong: <br>".$mail->ErrorInfo;
}
exit(json_encode(array("status" => $status, "response" => $response)));
}
我確實(shí)收到了& quot消息發(fā)送成功!"網(wǎng)站上的消息。
我已經(jīng)檢查了& quot將數(shù)據(jù)從Ajax傳輸?shù)絧hp & quot帖子,一切似乎都很好..
您沒有正確收集$_POST的值
$name = $POST['name'];
$email = $POST['email'];
$subject = $POST['subject'];
$message = $POST['message'];
試試這種方法
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];