【问题标题】:phpmailer but without hostphpmailer 但没有主机
【发布时间】:2018-11-03 22:28:23
【问题描述】:

以前我在 PHP 中使用 mail() 函数来处理我的网站。现在我们得到了站点的域(nginx 服务器),现在 mail() 只是不想表现,总是返回 false。我为 mail() 设置了 PHP ini,正确的域为

所以...我求助于 PHPMailer,但它仍然无法正常工作...。 我收到此错误....
Could not instantiate mail function.

所以我的问题是出了什么问题?我没有 SMTP 主机...我想使用本地服务器作为邮件服务器,因为它以前工作过,我相信我有正确的设置。

与作曲家一起安装。

这是我的代码:

    require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/PHPMailer.php");
        require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/SMTP.php");
        require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/Exception.php");

        $emailer = new PHPMailer\PHPMailer\PHPMailer();
        $emailer->IsMail();



$emailer->SetFrom("noreply@my.domain");
                $emailer->FromName = "My Domain";
                $emailer->AddAddress($admin_data['email']);
                $emailer->isHTML(false);
                $emailer->Subject = "test";
                $emailer->Body = " test "
if(!$emailer->send()){
                    give_error("Send Email Error: " . $emailer->ErrorInfo);
                    return false;
                } 

【问题讨论】:

  • 您的 SMTP 主机可能是 localhost ?我使用自己的邮件程序...但听起来 PHPMailer 已配置为使用外部服务等。检查..../src/SMTP.php 文件。

标签: php email nginx phpmailer


【解决方案1】:

如果想要了解 PHPMailer 驱动的电子邮件发送失败的原因,请务必打开 PHPMailer 的 "SMTPDebug" 选项。

为此,请在实例化 PHPMailer 对象后将 SMTPDebug 属性设置为“3”。在您的代码中,它看起来像:

//Echo errors, server responses, client message, data, commands & connection status
$emailer->SMTPDebug = 3;

这将回显电子邮件在尝试完成发送时可能溢出的所有错误、服务器响应和客户端消息。有了这个,您应该能够更好地处理可能导致您的电子邮件误发或根本不发送的原因。

即使您目前正在尝试捕捉错误,如果不启用此选项,它也不会捕捉到信息的全部广度和深度。

不过,请记住,在投入生产之前关闭此选项。将 SMTPDebug 属性设置回 0 将确保您不会向最终用户泄露错误或重要的服务器信息。

//Will disable all debugging.
$emailer->SMTPDebug = 0

您可以在 PHPMailer 文档中找到有关 SMTPDebug 属性的更多信息,该文档可能在您的代码库中命名为 "PHPMailer-master/docs/"

【讨论】:

  • 这将无效,除非您使用 SMTP,而 OP 不是。
【解决方案2】:

点击故障排除指南的链接。为了使mail() 功能起作用,您需要安装本地邮件服务器 - 我推荐使用 postfix。这与您的代码无关。

也就是说,虽然使用本地邮件服务器是一个不错的选择,但使用 SMTP 到 localhost 比使用邮件功能更快更安全。请参阅 PHPMailer 文档了解原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2023-03-31
    • 2018-09-01
    相关资源
    最近更新 更多