php - 无法使用 PhpMailer 和 Codeigniter 通过 Outlook SMTP 发送电子邮件

标签 php codeigniter email outlook phpmailer

每当我尝试使用 Phpmailer 发送电子邮件(outlook SMTP)时,我都会收到此错误:

2020-06-05 18:45:05 Connection: opening to smtp.office365.com:587, timeout=300, options=array()
2020-06-05 18:45:05 Connection: opened
2020-06-05 18:45:05 SERVER -> CLIENT: 220 AM0PR10CA0059.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 5 Jun 2020 18:45:05 +0000
2020-06-05 18:45:05 SERVER -> CLIENT: 250-AM0PR10CA0059.outlook.office365.com Hello [82.165.86.40]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2020-06-05 18:45:05 CLIENT -> SERVER: STARTTLS
2020-06-05 18:45:05 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2020-06-05 18:45:05 SERVER -> CLIENT: 250-AM0PR10CA0059.outlook.office365.com Hello [82.165.86.40]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2020-06-05 18:45:05 CLIENT -> SERVER: AUTH LOGIN
2020-06-05 18:45:05 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2020-06-05 18:45:05 CLIENT -> SERVER: [credentials hidden]
2020-06-05 18:45:05 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2020-06-05 18:45:05 CLIENT -> SERVER: [credentials hidden]
2020-06-05 18:45:10 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [AM0PR10CA0059.EURPRD10.PROD.OUTLOOK.COM]
2020-06-05 18:45:10 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [AM0PR10CA0059.EURPRD10.PROD.OUTLOOK.COM]
SMTP Error: Could not authenticate.
2020-06-05 18:45:10 CLIENT -> SERVER: QUIT
2020-06-05 18:45:10 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2020-06-05 18:45:10 Connection: closed
SMTP Error: Could not authenticate.

我也尝试过使用 Codeigniter

Failed to authenticate password. Error:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

我尝试了两种方法,但没有成功...

我正在使用 codeigniter 默认邮件类

这是配置:

        $config = array(

            'smtp_crypto'  => 'tls', or 'startTls'
            'smtp_auth'  => true,
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.office365.com',
            'smtp_port' => 587,
            'smtp_timeout' => '7',
            'smtp_user' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="056068646c69456466666a706b712b666a68" rel="noreferrer noopener nofollow">[email protected]</a>',
            'smtp_pass' => 'emailPassword', or 'APPPASSWORD'
            'mailtype'  => 'html',
            'charset'   => 'iso-8859-1',
            'wordwrap'  => TRUE,
            'validation'   => TRUE
        );

这是 Phpmailer 配置:

$mail->isSMTP();
$mail->Host     = 'smtp.office365.com';
$mail->SMTPAuth = false;
$mail->Username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187d7579717458797b7b776d766c367b7775" rel="noreferrer noopener nofollow">[email protected]</a>';
$mail->Password = 'emailPassword'; or 'APPPASSWORD'
$mail->SMTPSecure = 'tls', or 'startTls',
$mail->Port     =  587

有人遇到过这样的问题吗? 寻求帮助!!!

最佳答案

我遇到了一些证书问题,这就是我最终使用它的方式:

<?php
$mailusername= "******";
$mailpassword= "******";
$mailhost= "smtp.office365.com";
$to="*******";
$subject="test";
$body="abcdef";


require_once('PHPMailer-master/class.phpmailer.php');
require_once('PHPMailer-master/class.smtp.php');


$mail = new PHPMailer();


$mail->IsSMTP();
$mail->IsHTML(true);
$mail->Host       = $mailhost;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->Username   = $mailusername;
$mail->Password   = $mailpassword;

$frommail = $mailusername;//$this->from;
$fromname = "Mailbot";
$mail->SetFrom($frommail, $fromname);


$address = $to;
$adrname = "";
$mail->AddAddress($address, $adrname);

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

if(!$mail->Send()) {
    //echo "Mailer Error: " . $mail->ErrorInfo;
    print( "sendViaSMTP() - failed to send email");
} else {
    print( "sendViaSMTP() - success sending email");
    // all good
}


?>

(1) 对我来说,棘手的部分是 SMTPOptions()

(2) 另一个棘手的部分是使用最新 PHPMailer() 库。因为 SMTPOptions() 仅受较新的 PHPMailer()

支持

(3) 使用“no-verify”选项是不安全的。但是,在某些服务器上由于设置/配置问题而需要。

更新了我的代码

我正在使用 PHPMailer

   public $Version = '5.2.8';

关于php - 无法使用 PhpMailer 和 Codeigniter 通过 Outlook SMTP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62235896/

相关文章:

javascript - 如何检查 API token 是否仍然有效

php - Jquery和PHP的break函数问题

php - 在 codeigniter 中使用 PHP 导出表中的对象数组

java - 为 HTML 电子邮件正文错误编码希伯来语文本

php - 如何在 Symfony2 中使用 Swiftmailer 将 PDF 附加到电子邮件

javascript - 带参数的 PHP 函数

php - Codeigniter 将数据从 Controller 传递到 View

php - 如何从 Codeigniter (nginx) 中删除 index.php

php - 为教程博客创建面包屑菜单导航的技巧

linux - 如何在zabbix中通过电子邮件发送触发持续时间