连接到 Gmail IMAP PHP“无法打开流”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11772240/
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
Connecting to Gmail IMAP PHP "Couldn't open stream"
提问by cream
There are lots of people having similar issues but no one is answering their questions. I have IMAP enabled in PHP, Using all the correct information. I don't see where I'm going wrong.
有很多人有类似的问题,但没有人回答他们的问题。我在 PHP 中启用了 IMAP,使用了所有正确的信息。我不明白我哪里出错了。
Here's my code:
这是我的代码:
$hostname = '{imap.gmail.com:995/imap/ssl/novalidate-cert}';
$username = '[email protected]'; $password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
print_r(imap_errors());
Not returning any errors other than:
不返回任何错误,除了:
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:995/imap/ssl/novalidate-cert} in /home/a8066360/public_html/test/imap.php on line 6
Cannot connect to Gmail: Can't connect to gmail-imap.l.google.com, 995: Connection timed out
警告:imap_open() [function.imap-open]:无法在线打开流 {imap.gmail.com:995/imap/ssl/novalidate-cert} 在 /home/a8066360/public_html/test/imap.php 6
无法连接到 Gmail:无法连接到 gmail-imap.l.google.com,995:连接超时
I've noticed that if I change the single quotes to `
我注意到,如果我将单引号更改为 `
shell_exec() has been disabled for security reasons...
出于安全原因,shell_exec() 已被禁用...
Please help!!!
请帮忙!!!
回答by Max
You need port 993, the SSL IMAPport.
您需要端口993,即 SSL IMAP端口。
Port 995 is the SSL POP3port.
端口 995 是 SSL POP3端口。
回答by Xhezairi
I think Gmail's IMAP can only be accessed on port 993.
我认为 Gmail 的 IMAP 只能在端口 993 上访问。
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";
回答by MrGapo
I had the same error and found a different solution. I have added debug info into host:
我遇到了同样的错误并找到了不同的解决方案。我已将调试信息添加到主机中:
"{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}INBOX";
When I read php error log, I found
当我阅读php错误日志时,我发现
Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0
Open link, and follow instructions. Search for
打开链接,然后按照说明操作。搜索
Your app might not support the latest security standards. Try changing a few settings to allow less secure apps access to your account.
您的应用可能不支持最新的安全标准。尝试更改一些设置以允许安全性较低的应用访问您的帐户。
Click on link and enable less secure app access.
单击链接并启用安全性较低的应用程序访问。
Then it works for me.
然后它对我有用。
回答by Amal
You have to enable the given option from Gmail account get connected to Gmail server from another device or web. https://myaccount.google.com/lesssecureapps
您必须启用 Gmail 帐户中的给定选项才能从其他设备或网络连接到 Gmail 服务器。 https://myaccount.google.com/lesssecureapps
回答by Ryan Steyn
You can setup 2 step authentication and then assign an APP password to use in your requests (just replace your password with the one provided for the app, your normal password doesn't change.).
您可以设置两步验证,然后分配一个应用程序密码以在您的请求中使用(只需将您的密码替换为应用程序提供的密码,您的正常密码不会更改。)。
This will help your script run from any host without google blocking it (due to a change in login location).
这将帮助您的脚本从任何主机运行,而不会被谷歌阻止(由于登录位置的更改)。
回答by Shahrukh Anwar
You can try the following code by giving a notlsargument and connecting the server like follows, if SSL is not applied.
notls如果未应用 SSL,您可以通过提供参数并按如下方式连接服务器来尝试以下代码。
$hostname = '{imap.YOUR_DOMAIN.com:143/imap/notls}INBOX';
$username = 'YOUR_USERNAME';
$password = 'YOUR_PASSWORD';
$inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
If it will not throw any error, this means that you are succesfully connected to server then. I hope this helps.
如果它不会抛出任何错误,则表示您已成功连接到服务器。我希望这有帮助。

