英文:
Data not transfering from Ajax to php to mail
问题
I followed this tutorial: https://www.youtube.com/watch?v=DKq1n-awLcw
On how to implement a working contact form in my website.
Everything went fine, until I sent the first test mails and discovered that the mails are indeed sent, but without any information: Empty mail received.
I'm not an expert in jQuery or PHP, but I get how it works, and I can't seem to find why it is not working as intended.
I tried to tweak some variables here and there, but without any results.
Here is the 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>
Here's the script:
<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>
And finally, here is the PHP document:
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)));
}
I do receive the "Message sent successfully!" message on the website.
I already checked the "Transfer data from Ajax to PHP" post, and everything seems to be fine so...
英文:
I followed this tutorial : https://www.youtube.com/watch?v=DKq1n-awLcw
On how to implement a working contact form in my website.
Everything went fine, until I sent the first test mails and discovered that the mails are indeed sent, but without any informations : Empty mail received.
I'm not an expert in jquery or php, but I get how it works, and I can't seem to find why it is not working as intended.
I tried to tweek some variables here and there, but without any results.
Here is the 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>
Here the script :
<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>
And finaly here is the PHP document :
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)));
}
I do receive the "Message sent successfully!" message on the website.
I already checked the "Transfer data from Ajax to php" post, and everything seems to be fine so..
答案1
得分: 0
你没有正确获取$_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'];
英文:
You are not correctly collecting the values of $_POST
$name = $POST['name'];
$email = $POST['email'];
$subject = $POST['subject'];
$message = $POST['message'];
try this way
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论