2016-07-29 23 views
3

我是PHP新手,嘗試使用註冊表單構建網站。用戶應該收到一封帶有確認鏈接的電子郵件(因爲我有一個免費的託管服務器,我正嘗試使用Gmail服務器)。事情是我正在努力與電子郵件的PHP代碼。我試圖用PHPMailer的功能如下:使用gmail無法使用php發送電子郵件 - 授權錯誤

<?php 
 
/** 
 
* This example shows settings to use when sending via Google's Gmail servers. 
 
*/ 
 

 
//SMTP needs accurate times, and the PHP time zone MUST be set 
 
//This should be done in your php.ini, but this is how to do it if you don't have access to that 
 
date_default_timezone_set('Etc/UTC'); 
 
require 'PHPMailer/PHPMailerAutoload.php'; 
 

 
//Create a new PHPMailer instance 
 
$mail = new PHPMailer; 
 

 
//Tell PHPMailer to use SMTP 
 
$mail->isSMTP(); 
 

 
//Enable SMTP debugging 
 
// 0 = off (for production use) 
 
// 1 = client messages 
 
// 2 = client and server messages 
 
$mail->SMTPDebug = 2; 
 

 
//Ask for HTML-friendly debug output 
 
$mail->Debugoutput = 'html'; 
 

 
//Set the hostname of the mail server 
 
$mail->Host = 'smtp.gmail.com'; 
 
// use 
 
// $mail->Host = gethostbyname('smtp.gmail.com'); 
 
// if your network does not support SMTP over IPv6 
 

 
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
 
$mail->Port = 587; 
 
//Set the encryption system to use - ssl (deprecated) or tls 
 
$mail->SMTPSecure = 'tls'; 
 

 
//Whether to use SMTP authentication 
 
$mail->SMTPAuth = true; 
 

 
//Username to use for SMTP authentication - use full email address for gmail 
 
$mail->Username = "[email protected]"; 
 

 
//Password to use for SMTP authentication 
 
$mail->Password = "MYPASSWORD"; 
 

 
//Set who the message is to be sent from 
 
$mail->setFrom('[email protected]', 'Name S'); 
 

 
//Set an alternative reply-to address 
 
$mail->addReplyTo('[email protected]', 'Name S'); 
 

 
//Set who the message is to be sent to 
 
$mail->addAddress('[email protected]', 'John Doe'); 
 

 
//Set the subject line 
 
$mail->Subject = 'PHPMailer GMail SMTP test'; 
 

 
//Read an HTML message body from an external file, convert referenced images to embedded, 
 
//convert HTML into a basic plain-text alternative body 
 
$mail->msgHTML("HELLo"); 
 

 
//Replace the plain text body with one created manually 
 
//$mail->AltBody = 'This is a plain-text message body'; 
 

 
//send the message, check for errors 
 
if (!$mail->send()) { 
 
    echo "Mailer Error: " . $mail->ErrorInfo; 
 
} else { 
 
    echo "Message sent!"; 
 
} 
 
?>

我試過兩個端口587和465也試過SSL和TLS。每次我嘗試運行代碼我得到以下錯誤:

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45 - gsmtp CLIENT -> SERVER: EHLO spec.dr-manny.co.uk SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.36]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 CLIENT -> SERVER: AUTH LOGIN SERVER -> CLIENT: 334 VXNlcm5hbWU6 CLIENT -> SERVER: bWFubnlzYWVkaUBnbWFpbC5jb20= SERVER -> CLIENT: 334 UGFzc3dvcmQ6 CLIENT -> SERVER: a2luZ29uZW1vaDk5 SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP Error: Could not authenticate. CLIENT -> SERVER: QUIT SERVER -> CLIENT: 221 2.0.0 closing connection gw4sm17090153wjc.45 - gsmtp SMTP connect() failed. Error: SMTP connect() failed.

另外,我收到一封電子郵件來自Gmail的主題爲「有人取得了您的密碼」。我打開電子郵件,發現這個「
嗨Manny, 有人剛剛使用您的密碼嘗試使用應用程序(例如電子郵件客戶端或移動設備)登錄您的Google帳戶[email protected]。 我在運行php頁面後約15分鐘收到此電子郵件。

我關閉了「兩步驗證」並關閉了「允許安全性較低的應用程序」。似乎沒有任何幫助。任何人都可以幫助我嗎?

回答

2

去年我有同樣的問題:代碼(VB.Net)是好的,所以在哪裏憑據。問題在於,gmail不喜歡遠程(我的託管)的Web服務器中的某個應用程序,它試圖使用相同的用戶名。 (使用遠程桌面)在Gmail

之後,再次測試您的代碼。

+0

非常感謝!我嘗試了第二種選擇。有效!你救了我!謝謝:) –

+0

不客氣!這個難題在於錯誤信息並沒有真正告訴你問題是什麼:p – Roimer

+0

Thanx男人 - 它也適用於我 –

相關問題