I downloaded PHPMailer 5.2-stable from this link.
I uploaded the following files on my hosting: class.phpmailer.php, class.smtp.php and PHPMailerAutoload.php
Then I created a file name contact.php and wrote the following code in it:
require 'phpmailer/PHPMailerAutoload.php'; $mail = new PHPMailer(); $mail->SMTPDebug = 1; $mail->SMTPAuth = true; $mail->IsSMTP(); $mail->Host = "smtp.gmail.com"; $mail->Username = "*****************"; //My Email Address $mail->Password = "*****************"; //My Email's Address $mail->SMTPSecure = "tls"; $mail->Port = 578; $mail->AddAddress('*****************'); $mail->From = "*****************"; $mail->FromName = "Website Contact Form - " . "Website Name"; $mail->Subject = "New Message from Contact Form"; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail ->isHTML(true); $message = NULL; if(!$mail->Send()) { $message = "Mailer Error: " . $mail->ErrorInfo; }else{ $message = "Message sent!"; }
但是在运行这个文件后,我得到了以下错误:
2021-06-25 11:44:54 SMTP ERROR:无法连接服务器:网络不可达(101)SMTP connect() 失败。
我尝试了多个电子邮件地址,包括由我的托管服务直接创建的电子邮件地址、Gmail 帐户和 Google Workspace 邮件(以前称为 G Suit)。所有的都失败了,产生了上述错误。
最后,值得一提的是,我的托管服务不支持 PHP 的 mail() 函数,这就是我需要使用 SMTP 的原因。
更新:与主机联系并尝试了很多次后,虽然我确定输入了正确的用户名和密码,但我得到了以下错误:
2021-06-27 12:45:19 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: STARTTLS
2021-06-27 12:45:19 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: AUTH LOGIN
2021-06-27 12:45:19 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:19 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:20 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:20 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
2021-06-27 12:45:20 CLIENT -> SERVER: QUIT
2021-06-27 12:45:20 SERVER -> CLIENT: 221 2.0.0 closing connection u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
$mail->IsSMTP();
并添加$mail->SMTPSecure = 'ssl';
。 - Simone Rossaini