用PhpMailer通过gmail发送邮件

时间:2015-11-21 17:14:50

标签: php email xampp gmail phpmailer

我试图通过phpmailer用gmail帐户发送电子邮件。 这些页面托管在localhost xampp中。 PHP版本5.6.3

这是我的代码:

require_once 'Classes/class.phpmailer.php';
require 'Classes/class.smtp.php';

$mail = new PHPMailer();
//$mail->IsSMTP(); // Use SMTP
$mail->Host        = "smtp.gmail.com"; // Sets SMTP server
$mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
$mail->SMTPAuth    = TRUE; // enable SMTP authentication
$mail->SMTPSecure  = "tls"; //Secure conection
$mail->Port        = 587; // set the SMTP port
$mail->Username    = 'myemail@gmail.com'; // SMTP account username
$mail->Password    = 'myPassword'; // SMTP account password
$mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->Encoding    = '8bit';
$mail->Subject     = 'Test Email Using Gmail';
$mail->ContentType = 'text/html; charset=utf-8\r\n';
$mail->From        = 'myemail@gmail.com';
$mail->FromName    = 'GMail Test';
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true);
$mail->AddAddress('mywifeemail@gmail.com');
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$mail->Body    = "Hello";
   if(!$mail->Send())
      {
         echo "Mailer Error: " . $mail->ErrorInfo;
      }
      else
      {
         echo "Message sent!";
      }

以这种方式,页面响应是“消息发送!”,但我没有收到任何邮件。如果我取消注释

$mail->IsSMTP();

这是出现的错误;

2015-11-21 17:08:04 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q1sm4963515wjy.31 - gsmtp 2015-11-21 17:08:04 CLIENT -> SERVER: EHLO localhost 2015-11-21 17:08:04 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [93.55.36.161] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 2015-11-21 17:08:04 CLIENT -> SERVER: STARTTLS 2015-11-21 17:08:04 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2015-11-21 17:08:04 CLIENT -> SERVER: EHLO localhost 2015-11-21 17:08:05 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [93.55.36.161] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 2015-11-21 17:08:05 CLIENT -> SERVER: AUTH LOGIN 2015-11-21 17:08:05 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2015-11-21 17:08:05 CLIENT -> SERVER: cGFzcXVhbGUudHJpdGVsbGFAZ21haWwuY29t 2015-11-21 17:08:05 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2015-11-21 17:08:05 CLIENT -> SERVER: bW9ycm83NGRvcm8= 2015-11-21 17:08:05 SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 q1sm4963515wjy.31 - gsmtp 2015-11-21 17:08:05 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 q1sm4963515wjy.31 - gsmtp 2015-11-21 17:08:05 SMTP Error: Could not authenticate. 2015-11-21 17:08:05 CLIENT -> SERVER: QUIT 2015-11-21 17:08:05 SERVER -> CLIENT: 221 2.0.0 closing connection q1sm4963515wjy.31 - gsmtp 2015-11-21 17:08:05 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

我尝试了几种解决方案,但似乎无论如何都无法工作.... 任何想法如何解决? 提前谢谢

0 个答案:

没有答案