PHP smtp.office365.com 用于电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22623908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP smtp.office365.com for email
提问by bDir
Is it possible to make PHP application mail integrated with Office365?
是否可以将 PHP 应用程序邮件与 Office365 集成?
I have tried but found this error :
我试过但发现这个错误:
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)]
and my PHP code so far :
和我的 PHP 代码到目前为止:
$host = "smtp.office365.com";
$username = "[email protected]";
$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);
回答by user3531993
Several points:
几点:
- Email server names of the form
pod####.outlook.com
are the old standard and, while they still work for the time being, have been superseded by the genericsmtp.office365.com
. Just google "office365 smtp settings" and you'll see good documentation on this. - You'll need to specify
port 587
per the documentation you'll find from item 1. just as S. Varun states. While I've not reviewed your code in detail I suspect your problem is a lack of an SSL transport being loaded. The fix I'll describe is based on FreeBSD but you should be able to alter it to work on Windows (would help to know your environment):
Create a PHP file that contains the following code borrowed from PHP
stream_get_transports()
documentation:$xportlist = stream_get_transports(); print_r($xportlist);
When you execute this file in PHP (requires command line interface of PHP be present), if you don't see any mention of SSL, then this is your problem--no secure transports
Install PHP SSL module (I use ports in FreeBSD so I go to
security/php55-openssl
and domake install clean
)Restart Apache to make sure the new modules are loaded (ideally a graceful restart).
Now re-run your get_transports PHP code and you should see SSL listed (I got SSL, SSLv3, SSLv2, TLS in addition to the original protocols)
Now try your email sending code and it should work!
- 该表单的电子邮件服务器名称
pod####.outlook.com
是旧标准,虽然它们暂时仍然有效,但已被通用smtp.office365.com
. 只需谷歌“office365 smtp 设置”,您就会看到有关此内容的良好文档。 - 您需要根据
port 587
从第 1 项中找到的文档进行指定。正如 S. Varun 所述。 虽然我没有详细您的代码,但我怀疑您的问题是缺少正在加载的 SSL 传输。我将描述的修复基于 FreeBSD,但您应该能够将其更改为在 Windows 上工作(将有助于了解您的环境):
创建一个 PHP 文件,其中包含从PHP
stream_get_transports()
文档中借用的以下代码:$xportlist = stream_get_transports(); print_r($xportlist);
当您在 PHP 中执行此文件时(需要存在 PHP 的命令行界面),如果您没有看到任何提及 SSL,那么这就是您的问题——没有安全传输
安装 PHP SSL 模块(我在 FreeBSD 中使用端口,所以我
security/php55-openssl
去做make install clean
)重新启动 Apache 以确保加载新模块(理想情况下是正常重启)。
现在重新运行你的 get_transsports PHP 代码,你应该看到列出了 SSL(除了原始协议之外,我还获得了 SSL、SSLv3、SSLv2、TLS)
现在尝试您的电子邮件发送代码,它应该可以工作!
Hope this helps!
希望这可以帮助!
回答by clint
Assuming you have pear already installed (if you don't google how to install for your particular operating system) this code works perfectly for me in Ubuntu 12.04 LTS.
假设您已经安装了 pear(如果您不使用谷歌搜索如何为您的特定操作系统安装),此代码在 Ubuntu 12.04 LTS 中非常适合我。
If you get a "file not found for "Mail.php" then you need to change where your pear install directory is located.
如果您收到“找不到“Mail.php 的文件”,那么您需要更改您的 pear 安装目录所在的位置。
Issue this from the command line to find that out -> pear config-get php_dir . Once you know edit your include_path in php.ini.
从命令行发出此命令以找出 -> pear config-get php_dir 。一旦你知道在 php.ini 中编辑你的 include_path。
Don't forget in linux to use a : if you need to separate multiple includes and use a ; if your using windows php server to separate multiple includes. e.g. for linux: include_path = ".:/usr/share/php5:/usr/share/php"
不要忘记在 linux 中使用 : 如果您需要分隔多个包含并使用 ; 如果您使用 windows php 服务器来分隔多个包含。例如对于 linux:include_path = ".:/usr/share/php5:/usr/share/php"
<?php
require_once('/usr/share/php/Mail.php');
$from = "John Smith <[email protected]>";
$to = "Nancy Smith <[email protected]>";
$bcc = '';
$subject = "Hi!";
$body = "Hi,\n\nLooks like it worked.";
$host = 'smtp.office365.com';
$port = '587';
$username = 'Your Auth Username here.'; ##e.g. [email protected] 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>");
}
?>