2017-10-21 158 views
0

美好的一天,點證書不匹配「smtp.office354.com」使用PHPMailer的

我有我的PHPMailer的SMTP配置錯誤時smtp.office365.com

這裏我的腳本

require __DIR__ .'/vendor/phpmailer/phpmailer/src/Exception.php'; 
require __DIR__ .'/vendor/phpmailer/phpmailer/src/PHPMailer.php'; 
require __DIR__ .'/vendor/phpmailer/phpmailer/src/SMTP.php'; 

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 
use PHPMailer\PHPMailer\SMTP; 


$mail = new PHPMailer(true);        // Passing `true` enables exceptions 
try { 
    //Server settings 
    $mail->SMTPDebug = 4;         // Enable verbose debug output 
    $mail->isSMTP();          // Set mailer to use SMTP 
    $mail->Host = gethostbyname('smtp.office365.com'); // Specify main and backup SMTP servers 

    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
    $mail->SMTPAuth = true;        // Enable SMTP authentication 
    $mail->Username = 'office365 username';     // SMTP username 
    $mail->Password = 'office365 password';       // SMTP password 
    $mail->SMTPOptions = array (
         'ssl' => array(
         // 'verify_peer' => false, 

         // 'verify_peer_name' => false, 
         // 'allow_self_signed' => true 
         )); 
    $mail->Port = 587;         // TCP port to connect to 

    //Recipients 
    $mail->From  = $mail->Username; 
    $mail->addAddress('[email protected]');    // Name is optional 
    //Attachments 

    //Content 
    $mail->isHTML(true);         // Set email format to HTML 
    $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'; 

    $mail->send(); 
    echo 'Message has been sent'; 
} catch (Exception $e) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} 

我得到這個錯誤

stream_socket_enable_crypto(): Peer certificate CN=`servername.com' did not match expected CN=`smtp.office365.com' 

如果我取消

$mail->SMTPOptions = array (
         'ssl' => array(
         // 'verify_peer' => false, 
         'peer_name'   => 'smtp.office365.com', 
         // 'verify_peer_name' => false, 
         // 'allow_self_signed' => true 
         )); 

我仍然得到驗證失敗..

即時通訊服務器配置爲別人做的服務器設置不是很好。但我的網站是在https上。

回答

1

想想這是什麼意思。您要求連接到指定的主機,但證書上的名稱不匹配。這意味着服務器配置錯誤(對於office365不太可能),或者您被重定向到使用不同名稱的其他服務器。這很可能,因爲它在大型託管服務提供商中非常普遍。如果您將SMTPDebug = 2設置爲錯誤消息鏈接提供的故障排除指南,則會顯示所有內容。

出現這種情況是的事情 - 這是主要的原因使用TLS之一 - 它不僅加密在傳輸過程中您的流量,但提供了保證連接到服務器,你是一個你所料,即它正在做好自己的工作。

+0

還一件事..它實際上是工作時,我的第一天配置,然後一個星期後,我再檢查它,然後它開始產生這個錯誤..即時通訊真的不知道爲什麼.. – melvnberd

+1

權 - 這意味着您的SMTP連接被截獲並在其他地方重定向,因此名稱不再匹配。這就是TLS正確地完成工作,防止中間人遭受攻擊,並且停止向不知名的第三方頒發證書。檢查你的ISP他們的出站SMTP策略。你可能被迫使用他們的服務器,如果你想使用地址的Gmail,這是個壞消息。 – Synchro

+0

謝謝你的想法..也沒有使用Gmail作爲從,即時通訊實際上使用office365帳戶..這是否會影響問題先生? – melvnberd

相關問題