gpt4 book ai didi

php - 使用 phpmailer 发送表单时没有页面刷新

转载 作者:行者123 更新时间:2023-11-28 00:32:51 24 4
gpt4 key购买 nike

尝试将 phpmailer 与允许您发送表单而无需刷新页面的功能相结合。通常一切看起来都很好,只是电子邮件没有发送。在有关 phpmailer 的清晰代码上,一切正常。

我希望无需刷新页面即可发送电子邮件。也许有人遇到过类似的问题?

index.html

<form name="ContactForm" method="post" action="">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name">
</div>
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" class="form-control" id="message"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

<div class="message_box" style="margin:10px 0px;">
</div>
<script>
$(document).ready(function() {
var delay = 2000;
$('.btn-default').click(function(e){
e.preventDefault();
var name = $('#name').val();
if(name == ''){
$('.message_box').html(
'<span style="color:red;">Enter Your Name!</span>'
);
$('#name').focus();
return false;
}

var email = $('#email').val();
if(email == ''){
$('.message_box').html(
'<span style="color:red;">Enter Email Address!</span>'
);
$('#email').focus();
return false;
}
if( $("#email").val()!='' ){
if( !isValidEmailAddress( $("#email").val() ) ){
$('.message_box').html(
'<span style="color:red;">Provided email address is incorrect!</span>'
);
$('#email').focus();
return false;
}
}

var message = $('#message').val();
if(message == ''){
$('.message_box').html(
'<span style="color:red;">Enter Your Message Here!</span>'
);
$('#message').focus();
return false;
}

$.ajax({
type: "POST",
url: "ajax.php",
data: "name="+name+"&email="+email+"&message="+message,
beforeSend: function() {
$('.message_box').html(
'<img src="Loader.gif" width="25" height="25"/>'
);
},
success: function(data)
{
setTimeout(function() {
$('.message_box').html(data);
}, delay);

}
});
});
});
</script>

ajax.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// if(isset($_POST['submit'])){
// $name= $_POST['name'];
// $email= $_POST['email'];
// $tel= $_POST['tel'];
// $message= $_POST['message'];
if ( ($_POST['name']!="") && ($_POST['email']!="")){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailtrap.io'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bf4908e76c4186'; // SMTP username
$mail->Password = 'fe1e3963078670'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to

//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('pawel@gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('pawel@gmail.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');

//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $message;


$mail->send();
if($send){
echo "<span style='color:green; font-weight:bold;'>
Thank you for contacting us, we will get back to you shortly.
</span>";
}
else{
echo "<span style='color:red; font-weight:bold;'>
Sorry! Your form submission is failed.
</span>";
}
}

向我显示副标题:“感谢您联系我们,我们会尽快回复您。”但不幸的是邮箱不是空的。

最佳答案

除了 try/catch 问题,您还有其他问题。

SMTPSecure = 'tls'Port = 465 的组合将不起作用;要么更改为 ssl 模式,要么更改 Port = 587。这在故障排除指南中有详细记录。

不要使用提交者的地址作为发件人地址;这是伪造的,会导致您的邮件因 SPF 失败而被退回或被垃圾邮件过滤。将您自己的地址放在表单地址中,并将提交者的地址放在回复中 - 请参阅 PHPMailer 提供的联系表单示例。

关于php - 使用 phpmailer 发送表单时没有页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com