请不要确保我已检查过此类型的所有可能的问题,然后才找到答案,并且看起来好像遇到了麻烦。我不是新开发人员。但是我没有php语言。我可以做基本的事情。如果有一些对 PHP 和 PHPMailer 有经验的人可以提供帮助,将非常感谢。
我有3个网站。他们大量使用联系表格。我一直在为所有人共享相同的php代码,并且一直在工作,直到昨天我发现邮件程序实际上没有发送邮件。
当我检查错误日志时。这是我发现的。
[05-Feb-2019 15:41:03 Africa/Johannesburg] PHP Fatal error: Uncaught phpmailerException: Could not instantiate mail function. in /home/lovecharmking/public_html/phpmailer/class.phpmailer.php:1509
Stack trace:
#0 /home/lovecharmking/public_html/phpmailer/class.phpmailer.php(1346): PHPMailer->mailSend('Date: Tue, 5 Fe...', '<span><b>Name: ...')
#1 /home/lovecharmking/public_html/phpmailer/class.phpmailer.php(1215): PHPMailer->postSend()
#2 /home/lovecharmking/public_html/contact-form.php(47): PHPMailer->send()
#3 {main} thrown in/home/lovecharmking/public_html/phpmailer/class.phpmailer.php on line 1509.
这是我的代码。
<?php
ini_set('display_errors', '1');
require 'PHPMailerAutoload.php';
$contactEmail = $_POST['Email'];
$email2Spa = $_POST['Email2'];
$contactName = $_POST['Name'];
$contactNumber = $_POST['Cell'];
$reason = $_POST['Subject'];
$contactLocation = $_POST['Location'];
$contactMessage = $_POST['Message'];
if(!empty($email2Spa)) die();
$mail = new PHPMailer(true);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
$mail->Host = 'mail.domainname.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'help@domainname.com'; // SMTP username
$mail->Password = '**********'; // SMTP password 'TJ&ShBW[H*N#'
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('help@domainname.com', $contactName);
$mail->addAddress('help@domainname.com', 'Site Owner'); // Add a recipient
$mail->addReplyTo($contactEmail);
$mail->isHTML(true); // Set email format to HTML
$email_body = "";
$email_body .= "<span><b>Name: </b> " . $contactName . "</span><br/><br/>";
$email_body .= "<span><b>Reason: </b> " . $reason . "</span><br/><br/>";
$email_body .= "<span><b>Email Address: </b>" . $contactEmail . "</span><br/>";
$email_body .= "<p>Phone Number: " . $contactNumber . "</p>";
$email_body .= "<p><b>Location: </b>" . $contactLocation . "</p>";
$email_body .= "<p><b>Message: </b>" . $contactMessage . "</p>";
// $mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->AddCustomHeader("Importance: High");
$mail->Subject = $reason;
$mail->Body = $email_body;
if(!$mail->send()) {
// echo 'Mailer Error: ' . $mail->ErrorInfo;
// $response = array('success'=>"successfully send", 'message'=>"Message sent.");;
echo json_encode(array('success' => false, "Mailer Error: ".$mail->ErrorInfo));
} else {
// return $data['success'] = true;
// echo json_encode($data);
// echo 'Message has been sent.';
echo json_encode(array('success' => true, 'message' => "You message has been sent"));
}.
这是Ajax调用:
$('#cform').submit(function(e){
e.preventDefault();
const formUrl = 'contact-form.php';
let formData = $('#cform').serialize(); // Collect data from form
$.ajax({
type: "POST",
url: formUrl,
data: formData,
timeout: 6000,
error: function (request, error) {
console.log(error);},
success: function (data) {
var response = JSON.parse(data);
// console.log(response);
if (response.success==true) {
let alertDiv = document.createElement('div');
alertDiv.innerHTML = `
<strong>Your Message has been Sent!</strong> Admin will get back to you as soon as he is available.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>`
$('.alert').alert();
alertDiv.setAttribute('class','alert alert-warning alert-dismissible fade show alertBox');
alertDiv.setAttribute('role','alert');
// alertDiv.setAttribute('style','display:inline-block');
let messageSpan = document.querySelector('.alertMessage');
let parentForSpan = messageSpan.parentNode;
parentForSpan.replaceChild(alertDiv,messageSpan);
// contactForm.appendChild(alertDiv);
$('#cform')[0].reset();
// alertDiv.remove()
// let span = document.createElement('span');
// span.className = "alertMessage"
// console.log(response);
} else {
console.log('Something wrong is going on. Check it.');
}
return false
}});
return false;
});
这是ajax请求在浏览器中返回的内容:
Uncaught SyntaxError: Unexpected token C in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (main.js:212)
at c (jquery.min.js:4)
at Object.fireWith [as resolveWith] (jquery.min.js:4)
at k (jquery.min.js:6)
at XMLHttpRequest.r (jquery.min.js:6)
就像我说过的那样,我在这三个站点之间共享代码,但是它们都不起作用。他们都已经运行了多年。任何输入将不胜感激。
答案 0 :(得分:0)
这都是由以下建议引起的:
$mail->Mailer = “smtp”; // don't change the quotes!
那是完全错误的。这些弯引号可能会导致Mailer
设置为一些未定义的值,这意味着它将退回到使用mail()
进行发送。无论如何,您都不需要这样做,因为您先前对isSMTP()
的调用正确设置了Mailer
(假设您确实确实想使用SMTP),因此删除该行可能会有所帮助。
此处与PHPMailer相关的几乎每个答案都包含指向the troubleshooting guide的链接,该链接告诉您如何处理此错误:切换到SMTP或安装邮件服务器。它还会告诉您使用最新版本(不是),并将代码基于所提供的示例(而不是所使用的错误,过时,误导性的示例-我想知道您从何处获得了它。所以我可以请他们把它取下来。
如果您是PHP新手(但不是编码人员),那么您应该做的第一件事是learn how to use composer; PHP的作曲者是ruby的宝石,npm是node的宝石,pip是python的宝石;使用它来管理您的依赖项,例如PHPMailer。
在处理ajax处理程序时,请在之前检查您的后端是否产生可理解的结果(例如,有效的JSON),因为在服务器端出现错误是很常见的发送给客户端(发生了什么事),通常无法从JS进行解析,因此您遇到的是两个问题,而不是一个。