PHP尝试安全地发送邮件-找不到PHPMailer类 [英] PHP trying to send mail securely - cannot find the PHPMailer class

查看:228
本文介绍了PHP尝试安全地发送邮件-找不到PHPMailer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从以下内容开始:

composer require phpmailer/phpmailer

随后需要一次放置

require_once "../vendor/autoload.php";

然后

$mail = new PHPMailer;

这是PHP发生致命错误的地方:

That's where PHP has a fatal error:

致命错误:找不到类'PHPMailer'

Fatal error: Class 'PHPMailer' not found

以下是我的代码: 使用PHPMailer \ PHPMailer \ PHPMailer; 使用PHPMailer \ PHPMailer \ Exception;

The following is my code: use PHPMailer\PHPMailer\PHPMailer; use 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,因此它从未到达那里.

$htmlMessage is a variable containing both plain text and html with mime_boundary but it never gets there as it cannot find PHPMailer.

这是我试图解决此问题的方法:

This is what I have tried to do to fix the issue:

我在与供应商相同的目录中创建了另一个名为PHPMailer的目录,该新目录包含phpmailer/phpmailer/src的内容,但仍然找不到PHPMailer类.

I have created another directory called PHPMailer in the same directory as vendor, this new directory containing the contents of phpmailer/phpmailer/src, but it still cannot find the PHPMailer class.

我还需要PHPMailer.php文件吗?我以为那应该是自动加载功能?

Do I have to require the PHPMailer.php file as well? I thought that was what the autoload was supposed to do?

预先感谢

@Phil在评论中指出我缺少使用名称空间后,我更新了代码,这导致了新的错误:

After @Phil in the comments pointed out that I was missing the use namespace, I have updated the code, which has lead to a new error:

致命错误:未捕获的异常'PHPMailer \ PHPMailer \ Exception'和消息'SMTP connect()失败

Fatal error: Uncaught exception 'PHPMailer\PHPMailer\Exception' with message 'SMTP connect() failed

我了解到$mail->SMTPDebug = 3;提供了更多细节,其中揭示了以下内容:

I read that $mail->SMTPDebug = 3; gives more details, which revealed the following:

2019-02-11 01:51:57连接:打开ssl://smtp-relay.gmail.com:587,超时= 300,options = array()
2019-02-11 01:51:58连接失败.错误#2:stream_socket_client():SSL操作失败,代码为1.OpenSSL错误消息:error: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 connect()失败. https://github.com/PHPMailer/PHPMailer/wiki/疑难解答

2019-02-11 01:51:57 Connection: opening to ssl://smtp-relay.gmail.com:587, timeout=300, options=array()
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp-relay.gmail.com:587 (Unknown error) [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

然后我阅读另一篇文章更改以下设置,这使我看到它正在连接,但它不接受我的凭据:

I then read to on another post to change the following settings, which allowed me to see that it was connecting, but it was not accepting my credentials:

$mail->SMTPSecure = "tls";

2019-02-11 02:00:26服务器->客户:535-5.7.8不接受用户名和密码.在535 5.7.8中了解更多信息 https://support.google.com/mail/?p= BadCredentials f2sm562482otb.6-gsmtp
2019-02-11 02:00:26 SMTP错误:密码命令失败:535-5.7.8用户名和密码不被接受.在535 5.7.8中了解更多信息 https://support.google.com/mail/?p= BadCredentials f2sm562482otb.6-gsmtp

2019-02-11 02:00:26 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp
2019-02-11 02:00:26 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp

我目前不知道该怎么办,因为我知道这些是我的gmail帐户的凭据.我必须有一个Gmail企业帐户或类似的东西吗?

I do not know what to do at this point, as I know these are the credentials for my gmail account. Do I have to have a gmail business account or something like that?

我使用了错误的密码.输入正确的密码后,我开始收到以下消息:

I was using incorrect password. Once I entered the correct password, I began receiving the following message:

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电子邮件是从未在550-5.7.0中注册的域或IP地址发送的您的G Suite帐户.请登录您的G Suite帐户,并550-5.7.0确认您的发送设备IP地址已在550-5.7.0的G Suite SMTP中继设置中注册.有关更多信息,请访问550 5.7.0 https://support.google.com/a /answer/6140680#maildenied h192sm1484247qke.14-gsmtp

2019-02-11 14:33:44 SERVER -> CLIENT: 550-5.7.0 Mail relay denied [172.117.2.44]. Invalid credentials for relay for550-5.7.0 one of the domains in: (as obtained from HELO and MAIL FROM).550-5.7.0 Email is being sent from a domain or IP address which isn't registered550-5.7.0 in your G Suite account. Please login to your G Suite account and550-5.7.0 verify that your sending device IP address has been registered within550-5.7.0 the G Suite SMTP Relay Settings. For more information, please visit550 5.7.0 https://support.google.com/a/answer/6140680#maildenied h192sm1484247qke.14 - gsmtp

原来,我必须有一个G-Suite帐户才能使用smtp-relay.gmail.com.我只有一个普通的Gmail帐户.当我更改为smtp.gmail.com时,我又回到了错误的凭据错误.我将等待我的G-Suite帐户得到验证,然后将结果更新.

Turns out that I have to have a G-Suite account to use smtp-relay.gmail.com. I only have a regular gmail account. When I changed to smtp.gmail.com, then I am back to the bad credentials errors. I will just wait for my G-Suite account to be verified and I will update with the results.

推荐答案

尝试添加

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

在文件顶部的

导入PHPMailer类.

at the top of your file to import the PHPMailer classes.

这篇关于PHP尝试安全地发送邮件-找不到PHPMailer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆