簡體   English   中英

phpmailer和php5.6無法與office365一起使用

[英]phpmailer and php5.6 not working with office365

我有一個托管了機架空間的網站,並將我的網站技術從php5.4升級到了php5.6。 現在,我不再可以從該站點發送電子郵件。 最糟糕的部分是我什至沒有收到任何錯誤,並且日志什么也沒顯示。 我正在使用phpmailer 5.2.14。

這是我的郵件腳本

require 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['contactForm'])) {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $body = '
        <html>
            <body>
                <div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
                    <div style="display:inline-block; vertical-align:top;">
                        <a href="http://website.com"><img src="img/logoEmail.png" alt="waesf"></a>
                    </div>
                </div>
                <main style="float:left; width:100%; padding:20px;">
                    <p style="font-family:Arial; font-size:18px;">'.$message.'</p>
                </main>
            </body>
        </html>';
        $mail = new PHPMailer;

        $mail->SMTPDebug = 3;                                                         // Enable verbose debug output

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->isSMTP();                                                                // Set mailer to use SMTP
        $mail->Host = 'smtp.office365.com';                                             // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                                         // Enable SMTP authentication
        $mail->Username = 'person@email.com';                                 // SMTP username
        $mail->Password = 'secret';                                                // SMTP password
        $mail->SMTPSecure = 'tls';                                                      // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                                              // TCP port to connect to

        $mail->SetFrom('person@email.com', 'Ballpoint Machinist');
        $mail->AddAddress('person@email.com', "BPM");                         // Add a recipient
        $mail->addReplyTo('person@email.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        // $mail->addAttachment('/var/tmp/file.tar.gz');                                // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');                           // Optional name
        $mail->isHTML(true);                                                            // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->AltBody = $body;

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

當腳本運行時,站點會掛起一會兒,然后說服務器超時了。 沒有php錯誤代碼,什么也沒有,當我檢查日志時,沒有與我的郵件代碼相關的錯誤。 但是,這僅在Office 365發生,當我將smtp設置更改為gmail時,我至少在頁面上得到了php錯誤。

我已經讀過很多關於該主題的主題,但是我聽不懂。 我還通過了https://github.com/PHPMailer/PHPMailer/wiki/疑難解答,並添加了SMTPOptions以排除ssl,但這沒有幫助。

我很困惑,我沒有得到任何錯誤。 我有SMTPDebug = 3和error_reporting(E_ALL)。

根據Willy Pt的建議進行編輯。 腳本運行服務器超時時仍然不起作用。

if (isset($_POST['contactForm'])) {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $body = '
        <html>
            <body>
                <div style="float:left; width:100%; margin:0 0 25px 0; padding:20px; background:#222; text-align:center;">
                    <div style="display:inline-block; vertical-align:top;">
                        <a href="http://website.com"><img src="img/logoEmail.png" alt="Ballpoint Machinist"></a>
                    </div>
                </div>
                <main style="float:left; width:100%; padding:20px;">
                    <p style="font-family:Arial; font-size:18px;">'.$message.'</p>
                </main>
            </body>
        </html>';
        $mail = new PHPMailer(true);

        // $mail->SMTPDebug = 4;                                                         // Enable verbose debug output

        try {

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->isSMTP();                                                                // Set mailer to use SMTP
        $mail->Host = 'smtp.office365.com';                                             // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                                         // Enable SMTP authentication
        $mail->Username = 'person@person.com';                                 // SMTP username
        $mail->Password = 'test';                                                // SMTP password
        $mail->SMTPSecure = 'tls';                                                      // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                                              // TCP port to connect to

        $mail->SetFrom('person@person.com', 'Ballpoint Machinist');
        $mail->AddAddress('person@person.com', "BPM");                         // Add a recipient
        $mail->addReplyTo('person@person.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        // $mail->addAttachment('/var/tmp/file.tar.gz');                                // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');                           // Optional name
        $mail->isHTML(true);                                                            // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->AltBody = $body;
        $mail->send();
        echo "Message Sent OK\n";
        /*if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }*/


        } catch (phpmailerException $e) {
            echo $e->errorMessage(); //Pretty error messages from PHPMailer
        } catch (Exception $e) {
          echo $e->getMessage(); //Boring error messages from anything else!
        }
    }
} 

有人可以幫忙嗎?

不知道這是否可以作為答案,但是我能夠使用gmail進行此操作。 完全相同的代碼只是使用gmail憑據和smtp主機而不是office365。 我確實必須設置gmail才能允許安全性較低的應用。

問題出在PHP5.6和自簽名證書驗證上。 使用PHP5.6,默認情況下會啟用證書驗證,並且證書不能自簽名。

正確的解決方法是用一個好的證書替換無效,配置錯誤或自簽名的證書。

或者,將其配置為使用非自簽名證書:

$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);

您也可以在php.ini中全局更改這些設置,但這是一個非常糟糕的主意。 PHP 5.6進行此更改的原因非常充分。

有時這種行為不是很明顯。 有時,在客戶端嘗試執行STARTTLS之后立即發出QUIT命令時,可能會出現加密失敗的情況。 如果看到這種情況,則應檢查證書或驗證設置的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM