我跟著這個教程:https://www.youtube.com/watch? v = DKq1n-awl CW 如何在我的網站實現一個工作聯系形式?
一切都很順利,直到我發送了第一封測試郵件,發現郵件確實發送了,但是沒有任何信息:收到了空郵件。
我不是jquery或php方面的專家,但我知道它是如何工作的,而且我似乎找不到為什么它不能像預期的那樣工作。
我試著到處尋找一些變量,但沒有任何結果。
以下是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)));
}
我確實收到了& quot消息發送成功!"網站上的消息。
我已經檢查了& quot將數據從Ajax傳輸到php & 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'];