【问题标题】:PHPMailer Error: Could Not Instantiate Mail Function Is not working [duplicate]PHPMailer错误:无法实例化邮件功能不起作用[重复]
【发布时间】:2019-08-23 11:43:22
【问题描述】:

我在从 PHPMailer 发送邮件时遇到问题。当我尝试发送消息时,我收到一条错误消息,即

无法发送消息。邮件程序错误:无法实例化邮件功能。

我的 SMTP 是正确的,我找不到问题所在。谁能帮我找到问题?谢谢

<?php

  require "autoload.php";           

  if(isset($_POST["submit"])){

    $mail = new PHPMailer(true);

    $sender = "demo@gmail.com";

    //SMTP
    $mail->Host = 'smtp.gmail.com';                       
    $mail->SMTPAuth = true;                    // Enable SMTP authentication
    $mail->Username = demo@gmail.com;          // SMTP username
    $mail->Password = '1234567';               // SMTP password
    $mail->SMTPSecure = 'tls';                 // Enable TLS 
    $mail->Port = 587;                         // TCP port to connect

    $mail->From = "demo@gmail.com";
    $mail->FromName = 'Mailer';
    $mail->setFrom(demo@gmail.com, 'Mailer');
    $mail->addAddress($_POST["receiver"]);     // Name is optional

    $mail->Subject = $_POST["subject"];
    $mail->Body    = $_POST["message"];

    if(!$mail->send()) {    
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }               
  }
?>

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    你已经设置了各种 SMTP 相关的属性,但是你没有告诉 PHPMailer 使用 SMTP!

    添加这一行:

    $mail->isSMTP();
    

    您还启用了异常(将 true 传递给构造函数),但没有将您的代码包装在 try/catch 块中。请参阅 PHPMailer 提供的示例以了解如何处理。

    【讨论】:

      【解决方案2】:

      请重新检查您的配置,例如,用户/密码,端口号 如果您使用 SSL,请在端口 465 上连接到 smtp.gmail.com。否则使用 587。

      还要确保打开不太安全的应用 https://support.google.com/accounts/answer/6010255?hl=en

      【讨论】:

        猜你喜欢
        • 2015-08-19
        • 2019-07-24
        • 1970-01-01
        • 2015-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        相关资源
        最近更新 更多