使用 Bluehost 对 PHPMailer 进行故障排除

Troubleshooting PHPMailer with Bluehost

尝试为我在 Bluehost 上托管的网站设置 PHPMailer,但经过一整天的研究和故障排除后,我就是无法正常工作。

我是初学者,所以提前为新手问题道歉,但我一直在阅读我能找到的所有内容(包括 this and this, as well as the PHPMailer docs)来解决这个问题,但似乎无法设置我的正确。非常感谢任何指导、关于我做错了什么的想法或调试方法。

这是我发现的有关 Bluehost SMTP 的内容。

Secure SSL/TLS Settings (Recommended)

Username               Your email address: john@example.com
Password                The password for that email account.
Incoming Server      mail.example.com*
Incoming Port          993 (IMAP) or 995 (POP3)
Outgoing Server      mail.example.com*
Outgoing Port           465 (SMTP)
Authentication          Password

*Replace example.com with your domain name.

以下是我在我的文件中使用的内容(删除了个人信息)。

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);     
try {
    //Server settings
    $mail->SMTPDebug = 2;             
    $mail->isSMTP();                  
    $mail->Host = 'mail.MYDOMAIN.com';  
    $mail->SMTPAuth = true;            
    $mail->Username = 'MYEMAIL@MYDOMAIN.com';   
    $mail->Password = 'MYEMAILPASSWORD';        
    $mail->SMTPSecure = 'tls';                  
    $mail->Port = 465;                          

    //Recipients
    $mail->setFrom('MYEMAIL@MYDOMAIN.com');
    $mail->addAddress('MYEMAIL@MYDOMAIN.com'); 

    //Content
    $mail->isHTML(true);            
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我已经使用 Composer 安装了 PHPMailer,因此 autoload.php 文件(和 phpmailer 文件夹)位于 'vendor' 文件夹中,该文件夹与包含上述代码的文件位于同一目录中。

上传到我的 Bluehost 服务器后,当我尝试在浏览器中显示具有此代码的网页时,我得到了这个 HTTP ERROR 500 屏幕截图,当然没有发送电子邮件。

许多示例和 the docs 中涵盖的内容是加密和端口设置的组合将起作用。

您有 Port = 465SMTPSecure = 'tls';那是行不通的。要么将 Port 更改为 587,要么将 SMTPSecure 更改为 'ssl'(但 不是 !)。就目前而言,您正在尝试打开一个到需要隐式 TLS 的端口的连接,同时使用需要使用 STARTTLS 使其显式的协议,这是行不通的。