使用 Office365 SMTP 设置 PHPMailer

2024-02-03

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

Host: smtp.office365.com
Port: 587
Auth: tls

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

"host"      => "smtp.office365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "[email protected] /cdn-cgi/l/email-protection",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "[email protected] /cdn-cgi/l/email-protection",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

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


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

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

<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = '[email protected] /cdn-cgi/l/email-protection';
$mail->Password = 'YourPassword';
$mail->SetFrom('[email protected] /cdn-cgi/l/email-protection', 'FromEmail');
$mail->addAddress('[email protected] /cdn-cgi/l/email-protection', '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 <b>in bold!</b>';
$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';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Office365 SMTP 设置 PHPMailer 的相关文章