PHP中imap_open读取Office 365邮箱失败,如何启用TLS 1.2?
解决PHP imap_open连接Office 365的TLS 1.2配置问题
问题描述
自2022年10月15日起,使用PHP的imap_open函数读取Office 365邮箱时失败,报错信息:
Can not authenticate to IMAP server: AUTHENTICATE failed.
服务商建议使用TLS 1.2协议,但不清楚如何在代码中配置。原代码如下:
$username = $row->email; $password = $row->password; $mailbox = "{outlook.office365.com:993/imap/ssl/authuser=$username}"; $inbox = imap_open($mailbox, $username, $password); if (!$inbox){ print imap_last_error(); }
解决方案
修改IMAP连接字符串,将原有的/ssl替换为/tls,并通过sslproto=TLSv1.2强制指定使用TLS 1.2协议。
修改后的完整代码
$username = $row->email; $password = $row->password; // 强制使用TLS 1.2协议连接Office 365 IMAP服务 $mailbox = "{outlook.office365.com:993/imap/tls/sslproto=TLSv1.2/authuser=$username}"; $inbox = imap_open($mailbox, $username, $password); if (!$inbox){ print imap_last_error(); }
额外检查项
- 确认PHP环境的
openssl扩展已启用,且版本支持TLS 1.2(可通过phpinfo()查看OpenSSL相关配置)。 - 登录Office 365后台,确认该邮箱账号已开启IMAP协议支持。
- 验证账号密码是否正确,避免因凭证错误导致认证失败。
内容的提问来源于stack exchange,提问作者kumarsunit
相关产品推荐
相关产品推荐

