2017-09-18 153 views
0

我遇到與SMTP connect() failed PHPmailer - PHP相同的問題。我的代碼如下:如何用PHP發送電子郵件

public function sendEmail($toAddress, $subject, $message) 
{ 
    require 'PHPMailer-master/PHPMailerAutoload.php'; 
    $mail = new PHPMailer; 
    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true; 
    $mail->Username = 'username'; 
    $mail->Password = 'password'; 
    $mail->SMTPSecure = 'tls'; 
    $mail->SMTPDebug = 2; 
    $mail->Port = 587; 
    $mail->setFrom('[email protected]', 'Tripmatcher Support'); 
    $mail->addAddress($toAddress); 
    $mail->Subject = $subject; 
    $mail->Body = $message; 
    if (!$mail->send()) { 
     error_log('Mailer Error: ' . $mail->ErrorInfo); 
    } else { 
     error_log('Email has been sent.'); 
    } 
} 

兩個問題:

  1. 這裏有什麼問題,據我可以看到我的代碼看起來是正確的,而且我也啓用不夠安全的應用我的電子郵件地址?
  2. [Mon Sep 18 21:01:43.342766 2017] [:error] [pid 22052] [client 2.28.76.119:55132]郵件錯誤:SMTP connect()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting,referer:

這就是我在日誌中看到的所有內容,如何才能生成更具描述性的錯誤消息?我曾試着尋找類似的問題,但我仍然無法弄清楚究竟是什麼問題。

+1

您是否閱讀過您在問題中提到的頁面?特別是https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#smtp-error-could-not-connect-to-smtp-host和https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling -debug-output部分 – Nima

+0

dig + short smtp.gmail.com gmail-smtp-msa.l.google.com。 64.233.184.108 64.233.184.109 –

+0

這就是我從dig + short smtp.gmail.com得到的結果,而且我的SSL不適用於GoDaddy,所以我不認爲它是這兩者之一。 –

回答

0

對我來說,評論isSMTP()行使它工作,但它仍然進入我的Hotmail帳戶的垃圾郵件文件夾。