我正在尝试构建查询表单,并使用PHPMailer将数据发送到我的电子邮件中。我正在接收通过表单提交的数据,但无法将确认发送给填写表单的客户。到目前为止,这是我的表格:
<form class="myForm" action="" id="iForm" method="POST">
<input type="text" id="Name" placeholder="Name" required>
<input type="email" id="Email" placeholder="Email" required>
<input type="tel" id="Phone" placeholder="Phone Number" required>
<input type="text" id="Date" placeholder="Schedule a call" required>
<textarea id="Message" rows="5" placeholder="Your Message" required></textarea>
<div class="form-group">
<button type="submit" class="btn cbtn">SUBMIT</button>
</div>
</form>
传递提交表单中的数据
$("#iForm").on('submit', function(e) {
e.preventDefault();
var data = {
name: $("#Name").val(),
email: $("#Email").val(),
phone: $("#Phone").val(),
date: $("#Date").val(),
message: $("#Message").val()
};
if ( isValidEmail(data['email']) && (data['name'].length > 1) && (data['date'].length > 1) && (data['message'].length > 1) && isValidPhoneNumber(data['phone']) ) {
$.ajax({
type: "POST",
url: "php/appointment.php",
data: data,
success: function() {
$('.success.df').delay(500).fadeIn(1000);
$('.failed.df').fadeOut(500);
}
});
} else {
$('.failed.df').delay(500).fadeIn(1000);
$('.success.df').fadeOut(500);
}
return false;
});
检查有效的电子邮件地址
function isValidEmail(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
检查有效的电话号码
function isValidPhoneNumber(phoneNumber) {
return phoneNumber.match(/[0-9-()+]{3,20}/);
}
这是我从PHPMailer使用的代码:
$_name = $_REQUEST['name'];
$_email = $_REQUEST['email'];
$_phone = $_REQUEST['phone'];
$_date = $_REQUEST['date'];
$_message = $_REQUEST['message'];
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "office@myhost.co.uk";
$mail->Password = "********";
$mail->Host = "myhost.co.uk";
$mail->setFrom('office@myhost.co.uk', 'Alex');
$mail->addAddress('me@gmail.com', 'Alex');
$mail->isHTML(true);
$mail->Subject = 'New inquiry from CC';
$mail->Body = <<<EOD
<strong>Name:</strong> $_name <br>
<strong>Email:</strong> <a href="mailto:$_email?subject=feedback" "email me">$_email</a> <br> <br>
<strong>Phone:</strong> $_phone <br>
<strong>Booking Date:</strong> $_date <br>
<strong>Message:</strong> $_message <br>
我已经尝试过使用它,制作另一个PHPMailer实例,并使用他们提供的电子邮件将电子邮件发送给客户。
if($mail->Send()) {
$autoRespond = new PHPMailer();
$autoRespond->setFrom('office@myhost.co.uk', 'Alex');
$autoRespond->AddAddress($_email);
$autoRespond->Subject = "Autorepsonse: We received your submission";
$autoRespond->Body = "We received your submission. We will contact you";
$autoRespond->Send();
}
我已经尝试了一些在线“解决方案”,但是都没有成功。有什么想法吗?
答案 0 :(得分:1)
我终于找到了解决我问题的方法。基本上以上所有内容都是正确的,我唯一想念的就是再次将SMTP参数传递给新的PHPMailer实例。所以现在导致问题的最后一段代码看起来像这样:
if($mail->Send()) {
$autoRespond = new PHPMailer();
$autoRespond->IsSMTP();
$autoRespond->CharSet = 'UTF-8';
$autoRespond->SMTPDebug = 0;
$autoRespond->SMTPAuth = TRUE;
$autoRespond->SMTPSecure = "tls";
$autoRespond->Port = 587;
$autoRespond->Username = "office@myhost.co.uk";
$autoRespond->Password = "********";
$autoRespond->Host = "myhost.co.uk";
$autoRespond->setFrom('office@myhost.co.uk', 'Alex');
$autoRespond->addAddress($_email);
$autoRespond->Subject = "Autorepsonse: We received your submission";
$autoRespond->Body = "We received your submission. We will contact you";
$autoRespond->Send();
}
谢谢大家的帮助。