2014-12-04 174 views
0

我正在創建PHP類來發送電子郵件。問題是由於某些原因郵件沒有發送。有人知道問題在哪裏嗎?PHPMailer無法發送電子郵件

我正在這對XAMPP

下面是代碼:

include_once "class.sendMAIL.php"; 
$sendActivation = new sendMAIL(); 
$body="http://$_SERVER[HTTP_HOST]/activation.php?id=$activationCode"; 
if ($sendActivation->sendActivationEmail($name, $email,$body)) 
{ 
    echo "MAIL SENT"; 
} 
else { 
    echo "ERROR IN SENDING"; 
} 

郵件類:

require 'PHPMailerAutoload.php'; 
class sendMAIL { 
    private $sendEmail; 
    public function __construct() { 
     $this->sendEmail = new PHPMailer(); 
     $this->sendEmail->isSMTP();  // Set mailer to use SMTP 
     $this->sendEmail->Host = ' smtp.gmail.com'; // Specify main and backup SMTP servers 
     $this->sendEmail->SMTPAuth = true; // Enable SMTP authentication 
     $this->sendEmail->Username = '[email protected]'; // SMTP username 
     $this->sendEmail->Password = 'mypassword';   // SMTP password 
     $this->sendEmail->SMTPSecure = 'ssl';  // Enable encryption, 'ssl' also accepted 
     $this->sendEmail->port ="465"; 
     $this->sendEmail->From = ""; 
     //$this->sendEmail->FromName = $name; 
     $this->sendEmail->isHTML(true); 
    } 

    public function sendActivationEmail($name, $email, $link) { 
     $this->sendEmail->AddAddress($email); 
     $this->sendEmail->Subject="Aktivacija"; 
     $body="Follow this link to activate your account: <a href='$link' />LINK</a>"; 
     $this->sendEmail->MsgHTML($body); 
     if($this->sendEmail->send()) { 
      return TRUE; 
     } 
     else { 
      return FALSE; 
     }  
    } 
} 

THIS IS錯誤我得到了什麼當我調試的問題:

You must provide at least one recipient email address. 

謝謝到@paulgv我修正了錯誤,但現在我得到了這個錯誤:

SMTP connect() failed 

有人知道問題在哪裏嗎?

編輯:問題導致gmail。 Gmail阻止我的網絡應用程序訪問我的電子郵件。

+0

你怎麼知道它不發送? (不接收不一樣不發送) – 2014-12-04 21:41:08

+0

所以你得到虛假回來?那麼你應該檢查'$ this-> sendEmail-> ErrorInfo'的細節。和/或看看你的郵件服務器的日誌,看看/爲什麼郵件被拒絕 – 2014-12-04 21:42:02

+0

我試過......我調試,如果郵件從類發送調用sendMAIL類和輸出是假的 – jureispro 2014-12-04 21:42:09

回答

1

因此,這裏的答案,從我剛纔所發佈的評論複製;)

好吧,我也覺得這行代碼中的怪異:$this->sendEmail->AddAddress=($email);可能與$this->sendEmail->AddAddress($email);,而不是嘗試?

現在,關於您的SMTP身份驗證問題,您是否嘗試啓用詳細模式?可能有幫助 ! 根據文檔,你應該這樣做:

$this->sendEmail->SMTPDebug = 3; 
0

對於SMTP認證問題,試試這個:

$this->sendEmail->SMTPAuth = true;     // enable SMTP authentication 
$this->sendEmail->SMTPSecure = "tls";     // sets the prefix to the servier 
$this->sendEmail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$this->sendEmail->Port  = 587; 

它爲我工作。