2015-06-20 141 views
0

我正在嘗試使用phpmailer發送電子郵件。這是我寫的代碼。無法使用PHPMailer發送電子郵件

$mail = new PHPMailer; 

    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true; 
    $mail->Username = '[email protected]'; 
    $mail->Password = '*********'; 
    $mail->Port = 25; 

    $mail->From = '[email protected]'; 
    $mail->FromName = 'Shamir Towsif'; 
    $mail->addAddress('[email protected]', 'Shamir Towsif'); 
    $mail->addReplyTo('[email protected]', 'Information'); 

    $mail->isHTML(true); 

    $mail->Subject = 'Here is the subject'; 
    $mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

    if(!$mail->send()) { 
     echo "Message could not be sent.\n"; 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
     echo 'Message has been sent'; 
    } 

這是我得到的錯誤。

Message could not be sent. Mailer Error: SMTP connect() failed. 

我在做什麼錯。 SO中的其他問題沒有幫助。提前致謝。

+0

您是否對您的Google帳戶使用雙因素身份驗證?你確認你的密碼是正確的嗎?如果是這樣,請嘗試鏡像函數調用[這裏](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps)。 – jreut

+0

你確實閱讀了關於這個確切錯誤的800多個其他問題嗎?我印象深刻!我想[文檔](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)也沒有幫助。 – Synchro

+0

[SMTP連接()失敗的PHPmailer的可能重複 - PHP](http://stackoverflow.com/questions/22927634/smtp-connect-failed-phpmailer-php) – Synchro

回答

2

我面臨着類似的問題,但我想你應該嘗試添加該給你的代碼:

$mail->Port = 587; 
$mail->SMTPSecure = 'tls'; 

這是PHPMailer的推薦設置對於Gmail,你可以看到一個example in their Github page