2016-05-11 80 views
0

我使用cakePHP 3,當我將我的應用程序移動到服務器時,它停止發送電子郵件, 我使用gmail smtp服務器。我嘗試使用SSL連接到端口465上的smtp.gmail.com,但仍無法正常工作。另外變量$mail->SMTPDebug = on;正在造成一些麻煩。PHPmailer不使用gmail smtp與cakePHP 3

我的發送功能是這樣的:

public function send($to, $subject, $message) { 
    $sender = "[email protected]"; // this will be overwritten by GMail 

    $header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender"; 
    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "Content-Type: text/html; charset=UTF-8\r\n"; 

    $mail = new \PHPMailer(); 

    $mail->IsSMTP(); 
    $mail->Host = "aspmx.l.google.com"; 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = "ssl"; 
    $mail->Port = 25; 
    $mail->SMTPDebug = on; // turn it off in production 
    $mail->Username = "........"; 
    $mail->Password = "........"; 

    $mail->From = $sender; 
    $mail->FromName = "From Me"; 

    $mail->AddAddress($to); 

    $mail->IsHTML(true); 
    $mail->CreateHeader($header); 

    $mail->Subject = $subject; 
    $mail->Body = nl2br($message); 
    $mail->AltBody = nl2br($message); 

    // return an array with two keys: error & message 
    if(!$mail->Send()) { 
     return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo); 
    } else { 
     return array('error' => false, 'message' => "Message sent!"); 
    } 
} 
} 

我的錯誤是這樣的:

Use of undefined constant on - assumed 'on' [APP/Controller/Component/EmailComponent.php, line 30] 

Undefined variable: errno [ROOT/vendor/phpmailer/class.smtp.php, line 182] 

Undefined variable: errstr [ROOT/vendor/phpmailer/class.smtp.php, line 183] 

是可能的虛擬主機上,我有頁阻止它?

謝謝你像下面

回答

0

這裏發生了一些基本PHP語法的誤解。你有沒有試過reading the manual

在這種情況下,即使將其設爲字符串,on(以及簡單無效的PHP)也不是SMTPDebug的有效值。嘗試將其設置爲2,作爲the docs suggest。你的代碼是一團糟 - 你試圖破壞PHPMailer的一些部分(不要試圖像這樣設置頭部,讓PHPMailer正確地做);你已經將你的代碼基於一個過時的例子,並且正在使用舊版本的PHPMailer;你正在通過gmail發送錯誤的地址 - 它應該是smtp.gmail.com;您正嘗試在非SSL端口上使用SSL;您將HTML標籤放入純文本版本。

這裏有太多的錯誤值得修復,所以我建議你再次啓動一個clean, working example,並更新到the latest PHPMailer

只要問題連接到Gmail,這也很好地涵蓋了故障排除指南和其他許多問題。

+0

如果您的設備或應用程序支持SSL - 在端口465上連接到smtp.gmail.com。 如果您的設備或應用程序不支持SSL - 在端口25上連接到aspmx.l.google.com。 這就是我在https://support.google.com/a/answer/176600中看到了?hl = en 所以它取決於SSL支持 –

+0

正確,並且問題中的腳本使用端口25上的SSL連接到'aspmx.l.google.com',即它是錯誤的。線索在主機名 - 它包含'mx',表明它是一個入站服務器,而不是出站。 Google不會允許您在沒有身份驗證的情況下發送消息(它們不是垃圾郵件網關),並且您不希望在沒有加密的情況下使用auth,因此端口和該主機都不存在問題。 – Synchro

0

編輯您的線路:

$mail->Host = "smtp.gmail.com"; 
$mail->Port = 587; 

應該現在的工作。

+0

我試過了,還沒有工作。 –

0

在線路$mail->SMTPDebug = on; // turn it off in production

on必須在引號:'on'

UPDATE:

class.phpmailer.php(從線NR 315)存在SMTPDebug值的描述:這是整數從0到4.

// 0 - No output 
// 1 - Commands 
// 2 - Data and commands 
// 3 - As 2 plus connection status 
// 4 - Low-level data output 
+0

是的,謝謝,但仍然沒有發送 –

+0

引用修復了錯誤,但它仍然不是'SMTPDebug'的有效值。 – Synchro