php - 使用 Office365 SMTP 设置 PHPMailer

标签 php email ssl phpmailer office365

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

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

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

"host"      => "smtp.office365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@office365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@office365.com",
"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 = '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 <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';
}

关于php - 使用 Office365 SMTP 设置 PHPMailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24947434/

相关文章:

node.js - 如何在 Node js 中通过电子邮件附加 .ics 文件?

objective-c - 安装 Crashlytics 时出错 - SSL 对等握手失败

php - 根据选择的第一个下拉列表获取下拉列表的值

php - Laravel 5.2 将用户 IP 地址保存到数据库

php - 无法在数据库中插入必填字段

email - 发送电子邮件时出现 Postfix 错误 - 554 5.7.1 - 中继访问被拒绝

php - 如何在php中调用带有参数值的http post

ruby-on-rails - 如何测试发送数千封电子邮件

azure - 覆盖 Azure 中的现有 SSL 绑定(bind)

ios - 为什么 SecPKCS12Import 会自动将 SecIdentities 添加到钥匙串(keychain)中?