2016-01-15 207 views
1

我想下面的代碼來發送郵件 卻是露出無法發送電子郵件在PHP

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

和Gmail是通知我登入嘗試遭拒

<?php 
    require 'PHPMailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer; 

    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true; 
    $mail->Username = '[email protected]'; 
    $mail->Password = '*******'; 
    $mail->Port = 587; 
    $mail->SMTPSecure = 'tls'; 
    $mail->From = '[email protected]'; 
    $mail->FromName = 'asdf '; 
    $mail->addAddress('[email protected]', 'sadf '); 

    $mail->WordWrap = 50; 
    $mail->isHTML(true); 

    $mail->Subject = "Using PHPMailer"; 
    $mail->Body = "Hi Iam using PHPMailer library to sent SMTP mail from localhost"; 

    if(!$mail->send()) { 
     echo "Message could not be sent."; 
     echo "Mailer Error: " . $mail->ErrorInfo; 
     exit; 
    } 

    echo "Message has been sent"; 
    ?> 

如何解決上面的問題?

+0

請問您的主機的防火牆具有端口587傳出打開? – Blaatpraat

+0

出於好奇,你有沒有嘗試使用** ssl的端口465和25 **? – Torxed

+0

是兩個端口都顯示相同的問題 – xrcwrn

回答

2

您需要設置發送郵件的權限gmail

- 登錄谷歌帳戶
- 轉到隱私頁
- 允許第三方應用程序

試試這個代碼後:

$mail    = new PHPMailer(); 

$body    = file_get_contents('contents.html'); 
$body    = eregi_replace("[\]",'',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "mail.yourdomain.com"; // SMTP server 
$mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "tls";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 587;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "yourpassword";   // GMAIL password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 


if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
+0

完成仍然相同的消息 – xrcwrn

+0

我更新我的文章。 – rdn87

+0

https://accounts.google.com/b/0/DisplayUnlockCaptcha解決問題 – xrcwrn

1

改變你的代碼如下

isSMTP()IsSMTP(),addAddress()AddAddress() & isHTML()IsHTML()

並且是檢查端口也。有些東西的端口也關閉,不讓連接建立。

希望它能工作!

2

嘗試以下步驟:

  • 啓用調試模式捕捉可能出現的錯誤

    $mail->SMTPDebug = 1; 
    
  • 啓用SMTP認證功能

    $mail->SMTPAuth = true; 
    
  • 也爲您在PHP配置SSL支持文件(php.ini

    extension=php_openssl.dll 
    
+0

yes以上完成 – xrcwrn

1

我認爲你必須在你GAMIL啓用POP和IMAP。到Gmail

  • 在點擊右上角的齒輪試試這個

    • 登錄。
    • 選擇設置。
    • 點擊轉發和POP/IMAP。
    • 選擇啓用IMAP。
    • 單擊保存更改。

    <? 
     
    $account="[email protected]"; 
     
    $password="accountpassword"; 
     
    $to="[email protected]"; 
     
    $from="[email protected]"; 
     
    $from_name="Name"; 
     
    $msg="<strong>This is a bold text.</strong>"; // HTML message 
     
    $subject="Database Backup"; 
     
    /*End Config*/ 
     
    
     
    include("phpmailer/class.phpmailer.php"); 
     
    $mail = new PHPMailer(); 
     
    $mail->IsSMTP(); 
     
    $mail->CharSet = 'UTF-8'; 
     
    $mail->Host = "smtp.gmail.com"; 
     
    $mail->SMTPAuth= true; 
     
    $mail->Port = 465; // Or 587 
     
    $mail->Username= $account; 
     
    $mail->Password= $password; 
     
    $mail->SMTPSecure = 'ssl'; 
     
    $mail->From = $from; 
     
    $mail->FromName= $from_name; 
     
    $mail->isHTML(true); 
     
    $mail->Subject = $subject; 
     
    $mail->Body = $msg; 
     
    $mail->addAddress($to); 
     
    if(!$mail->send()){ 
     
    echo "Mailer Error: " . $mail->ErrorInfo; 
     
    }else{ 
     
    echo "E-Mail has been sent"; 
     
    } 
     
    ?>