繁体   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