php - 未捕获的 phpmailerException:无法实例化邮件功能
问题描述
请不要我已经确保我检查了之前提出的所有可能的此类问题以找到任何答案,并且看起来我遇到了障碍。我不是新开发人员。但我没有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)
就像我说的那样,我在 3 个站点之间共享代码,目前它们都不起作用。他们都运行了多年。任何输入将不胜感激。
解决方案
这都是由这个建议引起的:
$mail->Mailer = “smtp”; // don't change the quotes!
那是完全错误的。那些花引号可能会导致Mailer
被设置为一些未定义的值,这意味着它将回退到mail()
用于发送。无论如何您都不需要这样做,因为您之前的调用isSMTP()
设置Mailer
正确(假设您确实想要使用 SMTP),因此删除该行可能会有所帮助。
此处几乎所有与 PHPMailer 相关的答案都包含指向故障排除指南的链接,该指南告诉您如何处理此错误:切换到 SMTP 或安装邮件服务器。它还告诉您使用最新版本(您不是),并将您的代码基于提供的示例(不是您正在使用的不正确、过时、误导性的示例 - 我想知道您从哪里得到它所以我可以要求他们把它拿下来)。
如果您是 PHP 新手(但不是编码),您应该做的第一件事是学习如何使用 composer;Composer 之于 PHP 就像 gems 之于 ruby,npm 之于 node,pip 之于 python;使用它来管理您的依赖项,例如 PHPMailer。
处理 ajax 处理程序时,请在尝试从 JS 使用它之前检查您的后端是否产生了可理解的结果(例如有效的 JSON),因为将服务器端错误发送到客户端是很常见的(发生在您身上的事情) ,这通常不能从 JS 解析,然后你有两个问题而不是一个。
推荐阅读
- javascript - 如何通过 Ajax 传递多个变量?[Javascript 对象]
- android - 无法在 307200 字节的 TensorFlowLite 缓冲区和 270000 字节的 Java 缓冲区之间进行转换
- amazon-web-services - Cloudfront 备用域名 InvalidViewerCertificateException
- flutter - Flutter:导航到不同页面时保持状态
- reactjs - takeEvery 和 takeLatest。为什么?什么时候使用?同时使用?
- javascript - 尝试调用 readPixels 时出现无效类型错误
- mule - Datawave使用正则表达式提取字符串
- python - 如何使用 PySpark 进行特征选择/特征重要性?
- c++ - 逐行读取文本文件 | C++
- asp.net-core - .Net core 3.1 MVC 站点给出 http 403 错误