【问题标题】:PHP smtp.office365.com for email用于电子邮件的 PHP smtp.office365.com
【发布时间】:2014-03-25 01:36:48
【问题描述】:

是否可以将PHP应用邮件与Office365集成?

我试过但发现这个错误:

authentication failure [SMTP: SMTP server does not support authentication (code: 250, response: SINPR02CA025.outlook.office365.com Hello [17*.***.***.**] SIZE 78643200 PIPELINING DSN ENHANCEDSTATUSCODES STARTTLS 8BITMIME BINARYMIME CHUNKING)]

到目前为止我的 PHP 代码:

$host = "smtp.office365.com";
$username = "me@company.com";
$password = "example";

$headers = array(
    'From'          => $from,
    'To'            => $to,
    'Subject'       => $subject,
    'Content-Type'  => 'text/html; charset=UTF-8'
);

$recipients = $to.", ".$bcc;

$smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($recipients, $headers, $body);

【问题讨论】:

  • 尝试添加端口号或使用php mailer
  • 我不认为正确的地址是 smtp.office365.com 事实上,我最后一次这样做时我不得不联系微软,他们建议它是 podxxxxx.outlook.com
  • 有例子吗?

标签: php


【解决方案1】:

几点:

  • pod####.outlook.com 形式的电子邮件服务器名称是旧的 标准,虽然它们目前仍然有效,但已经 被通用 smtp.office365.com 取代。只是谷歌 “office365 smtp 设置”,你会看到很好的文档。
  • 您需要根据您找到的文档指定port 587 从第 1 项开始。正如 S. Varun 所说。
  • 虽然我没有审查过您的 详细代码我怀疑您的问题是缺少 SSL 传输 正在加载。我将描述的修复是基于 FreeBSD 但你 应该能够将其更改为在 Windows 上工作(将有助于了解 你的环境):

    1. 创建一个 PHP 文件,其中包含以下从 PHP stream_get_transports() documentation 借用的代码:

      $xportlist = stream_get_transports(); print_r($xportlist);

    2. 当你在 PHP 中执行这个文件时(需要有 PHP 的命令行界面),如果你没有看到任何提到 SSL,那么这就是你的问题——没有安全传输

    3. 安装 PHP SSL 模块(我在 FreeBSD 中使用端口,所以我转到 security/php55-openssl 并执行 make install clean

    4. 重启 Apache 以确保新模块已加载(最好是正常重启)。

    5. 现在重新运行您的 get_transports PHP 代码,您应该会看到列出的 SSL(除了原始协议之外,我还有 SSL、SSLv3、SSLv2、TLS)

    6. 现在试试你的电子邮件发送代码,它应该可以工作了!

希望这会有所帮助!

【讨论】:

  • Using Pear's Mail.php 这个答案导致了我的解决方案。我只需取消注释我的 ini 中的“extension=php_openssl.dll”行,因为显然已经安装了 openSSL。也是2020年的我,主机设置为“smtp.office365.com”。
  • 自 2018 年 10 月起,您的 stream_get_transports 应至少列出 tls_v1.2,详情请参见此处:dirteam.com/dave/2018/01/10/office-365-only-allows-tls-1-2 还要确保您的 PEAR 安装足够新,即它包含 Auth/SASL.php 文件。跨度>
【解决方案2】:

假设您已经安装了 pear(如果您不 google 如何为您的特定操作系统安装),此代码在 Ubuntu 12.04 LTS 中非常适合我。

如果您收到“找不到“Mail.php”的文件,那么您需要更改您的 pear 安装目录所在的位置。

从命令行发出这个以找出答案 -> pear config-get php_dir 。一旦你知道在 php.ini 中编辑你的 include_path。

不要忘记在 linux 中使用 : 如果您需要分隔多个包含并使用 ;如果您使用 windows php 服务器来分隔多个包含。例如对于 linux:include_path = ".:/usr/share/php5:/usr/share/php"

<?php
require_once('/usr/share/php/Mail.php');


$from = "John Smith <john@youroffice365emailhere.com>";
$to = "Nancy Smith <Nancy@youroffice365emailhere.com>";
$bcc = '';
$subject = "Hi!";
$body = "Hi,\n\nLooks like it worked.";

$host = 'smtp.office365.com';
$port = '587';
$username = 'Your Auth Username here.'; ##e.g. test@yourdomain.com you do not need on.microsoft.com or anything here. Use your real email address you use for authentication.
$password = 'Your Auth Password for the email address above';

$headers = array(
 'Port'          => $port,
 'From'          => $from,
 'To'            => $to,
 'Subject'       => $subject,
'Content-Type'  => 'text/html; charset=UTF-8'
);

$recipients = $to.", ".$bcc;

$smtp = Mail::factory('smtp',
 array ('host' => $host,
 'port' => $port,
 'auth' => true,
 'username' => $username,
 'password' => $password));

$mail = $smtp->send($recipients, $headers, $body);

echo "test";

if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
} else {
   echo("<p>Message successfully sent!</p>");
}
?>

【讨论】:

    猜你喜欢
    • 2022-11-15
    • 2018-08-17
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 2013-01-31
    • 2012-08-06
    相关资源
    最近更新 更多