使用 PHP 和 IMAP 连接到 Gmail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32222250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:51:28  来源:igfitidea点击:

Connect to Gmail with PHP & IMAP

phpsslgmailimap

提问by Makis

I have enabled OpenSSLand IMAPfunctions from my php.inifile and phpinfo()confirms it.

我已经从我的文件中启用OpenSSLIMAP运行php.iniphpinfo()确认它。

By using the code below i can connect to a Hotmail account but not to a Gmail account.(of course i change the $connect_toto point to Hotmail.)

通过使用下面的代码,我可以连接到 Hotmail 帐户,但不能连接到 Gmail 帐户。(当然我将其更改$connect_to为指向 Hotmail。)

$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$connection = imap_open($connect_to, $user, $password)
  or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);

The returned error is

返回的错误是

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6
Can't connect to '{imap.gmail.com:993/imap/ssl}INBOX': Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Notice: Unknown: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA (errflg=2) in Unknown on line 0

Unfortunately i cannot find a complete tutorial of how to use the IMAP functions.

不幸的是,我找不到有关如何使用 IMAP 功能的完整教程。

Any ideas, solutions or suggestions will be helpful.

任何想法、解决方案或建议都会有所帮助。

回答by Makis

UPDATE:

更新:

After searching for my problem on the Internet, i did not found a solution that solves my problem completely. Although if

在网上搜索我的问题后,没有找到完全解决我的问题的解决方案。虽然如果

1) I use the insecure $connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';

1)我使用不安全的 $connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';

2)And allow gmail to be accessed by less secured apps,

2)并允许gmail被访问less secured apps

solves my problem for now.

暂时解决了我的问题。

Please check this https://support.google.com/accounts/answer/6010255?hl=en

请检查此https://support.google.com/accounts/answer/6010255?hl=en

回答by Johan Velthuis

If you get blocked, this can be because google sees a login from a new device.

如果您被阻止,这可能是因为 google 看到了来自新设备的登录信息。

Go to your google account and check https://myaccount.google.com/device-activityand let them know the 'Unknown device is ok to use'.

转到您的 google 帐户并检查 https://myaccount.google.com/device-activity并让他们知道“可以使用未知设备”。

回答by Antony D'Andrea

First check your date and time settings on your server are correct.

首先检查您服务器上的日期和时间设置是否正确。

Alternatively try:

或者尝试:

$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX',

This will stop it from trying to validate certificate.

这将阻止它尝试验证证书。

I will probably get downvotes from people telling me this is bad practice as this leaves you open to Man in the Middle attacks. Which is true.

我可能会从那些告诉我这是不好的做法的人那里得到反对票,因为这会让你对中间人攻击持开放态度。这是真的。