【发布时间】:2019-07-04 11:36:23
【问题描述】:
我从以下开始:
composer require phpmailer/phpmailer
随后放置一次require
require_once "../vendor/autoload.php";
然后
$mail = new PHPMailer;
这就是 PHP 出现致命错误的地方:
致命错误:找不到类“PHPMailer”
以下是我的代码: 使用 PHPMailer\PHPMailer\PHPMailer; 使用 PHPMailer\PHPMailer\Exception;
# require php mailer
require_once "../vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
// $mail->SMTPSecure = "ssl";
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "some gmail address";
$mail->Password = "some password";
$mail->SetFrom("info@example.com");
$mail->Subject = $subject;
$mail->Body = $htmlMessage;
$mail->AddAddress($recpEmails);
$mail->Send();
$htmlMessage 是一个包含纯文本和带有 mime_boundary 的 html 的变量,但它永远不会到达那里,因为它找不到 PHPMailer。
这是我试图解决此问题的方法:
我在 vendor 目录下创建了另一个名为 PHPMailer 的目录,这个新目录包含 phpmailer/phpmailer/src 的内容,但它仍然找不到 PHPMailer 类。
我是否也需要 PHPMailer.php 文件?我认为这就是自动加载应该做的事情?
提前致谢
更新
在 cmets 中的@Phil 指出我缺少 use 命名空间后,我更新了代码,这导致了一个新的错误:
致命错误:未捕获异常 'PHPMailer\PHPMailer\Exception' 并带有消息 'SMTP connect() failed
我读到$mail->SMTPDebug = 3; 提供了更多细节,其中揭示了以下内容:
2019-02-11 01:51:57 连接:打开到 ssl://smtp-relay.gmail.com:587, timeout=300, options=array()
2019-02-11 01:51:58 连接失败。错误 #2:stream_socket_client():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议 [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php 第 327 行]
2019-02-11 01:51:58 连接失败。错误 #2:stream_socket_client():无法启用加密 [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php 第 327 行]
2019-02-11 01:51:58 连接失败。错误 #2:stream_socket_client():无法连接到 ssl://smtp-relay.gmail.com:587(未知错误)[v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php 第 327 行]
2019-02-11 01:51:58 SMTP 错误:无法连接到服务器:(0)
SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I then read to on another post 更改以下设置,这让我可以看到它正在连接,但它不接受我的凭据:
$mail->SMTPSecure = "tls";
2019-02-11 02:00:26 服务器 -> 客户端:535-5.7.8 用户名和密码不被接受。了解更多 at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp
2019-02-11 02:00:26 SMTP 错误:密码命令失败:535-5.7.8 用户名和密码不被接受。了解详情 at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp
此时我不知道该怎么做,因为我知道这些是我的 gmail 帐户的凭据。我必须有一个 gmail 企业帐户或类似的东西吗?
另一个更新
我使用了错误的密码。输入正确密码后,我开始收到以下消息:
2019-02-11 14:33:44 服务器 -> 客户端:550-5.7.0 邮件中继被拒绝 [172.117.2.44]。 550-5.7.0 中以下域之一的中继凭据无效:(从 HELO 和 MAIL FROM 获得).550-5.7.0 电子邮件正在从未注册的域或 IP 地址发送 550-5.7.0您的 G Suite 帐号。请登录您的 G Suite 帐户并 550-5.7.0 验证您的发送设备 IP 地址已在 G Suite SMTP 中继设置 550-5.7.0 中注册。更多信息请访问550 5.7.0https://support.google.com/a/answer/6140680#maildeniedh192sm1484247qke.14-gsmtp
原来我必须有一个 G-Suite 帐户才能使用 smtp-relay.gmail.com。我只有一个普通的 gmail 帐户。当我更改为 smtp.gmail.com 时,我又回到了错误的凭据错误。我将等待我的 G-Suite 帐户得到验证,然后更新结果。
【问题讨论】:
-
你读过他们的simple example吗?您缺少用于完全限定类名
PHPMailer\PHPMailer\PHPMailer的use语句 -
谢谢@Phil,至少我有一个新错误 - 致命错误:未捕获的异常 'PHPMailer\PHPMailer\Exception' with message 'SMTP connect() failed - 我将更新代码以反映
-
“我知道这些是我的 gmail 帐户的凭据”→ 表示您使用的是纯密码,并且您没有查看任何其他问题。
标签: php composer-php phpmailer