【问题标题】:Uncaught phpmailerException: Could not instantiate mail function未捕获的 phpmailerException:无法实例化邮件功能
【发布时间】:2019-06-29 09:29:28
【问题描述】:

请不要我确保我检查了之前提出的所有可能的此类问题以找到任何答案,并且看起来我遇到了障碍。我不是新开发人员。但我没有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">&times;</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 个站点之间共享代码,此时它们都无法正常工作。他们都运行了多年。任何意见将不胜感激。

【问题讨论】:

  • 我看到了这个问题
  • 但是这些答案中的任何一个都没有帮助。所以我真的认为这是一个不同的问题。如果你能帮忙的话。否则不要只标记为可能重复。我不能在这里浪费人们的时间。
  • 这就是为什么它被称为可能重复。我承认它没有帮助。
  • 这会影响我的问题排名吗? ?你认为这是一个流行版本的问题。从昨天开始就一直想解决这个问题。问题是所有 3 个站点现在都无法正常工作。我应该切换回 neonal php 功能吗?

标签: php email phpmailer contact-form


【解决方案1】:

这都是由这个建议引起的:

$mail->Mailer = “smtp”; // don't change the quotes!

这完全是错误的。这些花引号可能会导致Mailer 被设置为某个未定义的值,这意味着它将退回到使用mail() 进行发送。无论如何您都不需要这样做,因为您之前对 isSMTP() 的调用正确地设置了 Mailer(假设您确实想要使用 SMTP),因此删除该行可能会有所帮助。

这里几乎所有与 PHPMailer 相关的答案都包含指向 the troubleshooting guide 的链接,它会告诉您如何处理此错误:要么切换到 SMTP,要么安装邮件服务器。它还告诉您使用最新版本(您不是),并将您的代码基于提供的示例(不是您正在使用的不正确、过时、误导性的示例 - 我想知道您从哪里得到它所以我可以让他们把它拿下来)。

如果您是 PHP 新手(但不是编码),您应该做的第一件事是learn how to use composer; Composer 之于 PHP 就像 gems 之于 ruby​​,npm 之于 node,pip 之于 python;用它来管理你的依赖,比如 PHPMailer。

在处理 ajax 处理程序时,请检查您的后端是否产生了可理解的结果(例如有效的 JSON)在您尝试从 JS 使用它之前,因为服务器端错误很常见发送给客户端(发生在你身上的事情),通常无法从 JS 解析,然后你会遇到两个问题而不是一个。

【讨论】:

  • 非常感谢@Synchro。我将进行更改,如果成功报告回来。如果我记得是从 PHPMailer github 页面获得的代码。我非常感谢您的意见。
  • 非常感谢。 Mailer 现在已经成功发送邮件了。现在只是得到一个 json 语法错误。这条线的真正作用是什么? echo json_encode(array('success' => true, 'message' => "您的消息已发送"));
  • 非常感谢。 Mailer 现在已经成功发送邮件了。立即返回SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 5 of the JSON data。这条线的真正作用是什么?=> echo json_encode(array('success' =&gt; true, 'message' =&gt; "You message has been sent")); 我去从服务器返回一个对象,带有成功和消息属性。你能解释一下为什么它以前有效吗?
  • Json_encode 将 PHP 数组转换为 JSON 字符串表示。您的输出可能包含一些破坏 JSON 有效性的额外空格。仔细查看您的浏览器收到的确切响应。
  • 顺便说一句,您的代码显然基于官方示例之一,但它很旧并且包含主 github 项目中永远不会出现的错误 - 就像关于引号的评论 - 所以我怀疑它来自其他地方。
猜你喜欢
  • 1970-01-01
  • 2011-08-08
  • 2015-01-17
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 2021-04-26
相关资源
最近更新 更多