首页  >  问答  >  正文

配置 PHPMailer 以使用 Off ice365 SMTP

我正在尝试设置 PHPMailer,以便我们的一位客户能够从他们自己的帐户中自动生成电子邮件。我登录了他们的Of  fice 365帐户,发现PHPMailer所需的设置是:

Host: smtp.off   ice365.com
Port: 587
Auth: tls

我已将这些设置应用于 PHPMailer,但是没有发送电子邮件(我调用的函数适用于我们自己的邮件,该邮件是从外部服务器(不是服务网页的服务器)发送的)。

"host"      => "smtp.of   fice365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@off  ice365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@of  fice365.com",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

有谁知道PHPMailer通过smtp.offi   ce365.com发送需要什么设置?

P粉510127741P粉510127741338 天前670

全部回复(2)我来回复

  • P粉176151589

    P粉1761515892023-10-17 10:00:21

    更新:2022 年 5 月

    所以我在这个问题上苦苦挣扎。对于具有 Exchange Online 和 Microsoft 管理中心访问权限的企业帐户,我可以提供此问题的答案。

    TLDR:转到管理中心并选择您要发送邮件的用户。然后在设置“经过身份验证的 SMTP”之后查看“电子邮件”和“电子邮件应用程序”后的设置,只需启用它即可。

    仍然无法工作?我已经为您提供了帮助,这就是我如何让它完全发挥作用。

    1. 使用PHP Composer,实际上节省了很多工作。
    2. 将您的代码替换为我的代码,并在测试后更改
    SMTPDebug = SMTP::DEBUG_off;
    
    //SMTP
    $mail = new PHPMailer(true); //important
    $mail->CharSet = 'UTF-8';  //not important
    $mail->isSMTP(); //important
    $mail->Host = 'smtp.office365.com'; //important
    $mail->Port       = 587; //important
    $mail->SMTPSecure = 'tls'; //important
    $mail->SMTPAuth   = true; //important, your IP get banned if not using this
    
    //Auth
    $mail->Username = 'yourname@mail.org';
    $mail->Password = 'your APP password';//Steps mentioned in last are to create App password
    
    //Set who the message is to be sent from, you need permission to that email as 'send as'
    $mail->SetFrom('hosting@mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname@mail.org
    
    //Set an alternative reply-to address
    $mail->addReplyTo('no-reply@mail.com', 'First Last');
    
    //Set who the message is to be sent to
    $mail->addAddress('customer@othermail.com', 'SIMON MÜLLER');
    //Set the subject line
    $mail->Subject = 'PHPMailer SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML(file_get_contents('replace-with-file.html'), __DIR__);  //you can also use $mail->Body = "

    This is a body message in html

    " //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file //$mail->addAttachment('../../../images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { }
    1. 这可能看起来像您的文件,没关系,但现在是简单棘手的部分。与 Google 一样,Microsoft 也为 SMTP 实现了“开关”。只需从您的企业帐户转到您的管理中心,或者请有权限的人执行该部分操作即可:
    1. 如果您使用 MFA,请确保使用 https://stackoverflow.com/ 中提到的应用密码a/61359150/14148981

    2. 运行脚本

    我希望这对某人有帮助。我花了很长时间才找到这个选项。

    获取应用密码和身份验证的所有步骤摘要:

    1. 使用管理员帐户:
    • Active Directory AD -> 属性 -> 安全默认值:关闭。
    • Active Directory 门户 -> 条件访问 -> 配置 MFA 可信 IP -> 允许用户应用程序密码:启用
    • 管理页面用户列表 -> 目标用户的“多重身份验证”:启用然后强制
    • 管理页面用户列表 -> 用户详细信息 -> 邮件 -> “管理电子邮件应用程序” -> “经过身份验证的 SMTP”:启用
    1. 使用用户帐户:
    • 用户帐户配置文件 -> 安全 -> 添加登录方法:应用密码
    1. PHP 邮件程序设置:
    • smtp.office365.com、587、tls、电子邮件、appPassword

    回复
    0
  • P粉395056196

    P粉3950561962023-10-17 00:05:06

    @nitin 的代码对我不起作用,因为它在 SMTPSecure 参数中缺少“tls”。

    这是一个工作版本。我还添加了两行注释掉的行,您可以在出现问题时使用它们。

    isSMTP();
    $mail->Host = 'smtp.office365.com';
    $mail->Port       = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth   = true;
    $mail->Username = 'somebody@somewhere.com';
    $mail->Password = 'YourPassword';
    $mail->SetFrom('somebody@somewhere.com', 'FromEmail');
    $mail->addAddress('recipient@domain.com', 'ToEmail');
    //$mail->SMTPDebug  = 3;
    //$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
    $mail->IsHTML(true);
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

    回复
    0
  • 取消回复