php - 无法连接 :Can't open mailbox {imap. gmail.com:993/imap/ssl/novalidate-cert}收件箱:无效的远程规范

标签 php ssl gmail imap gmail-imap

多年来一直在寻找解决此错误的方法...

尝试保存来自 gmail 帐户的电子邮件附件,但不断收到错误消息:

Cannot connect to Gmail: Can't open mailbox {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX: invalid remote specification

来自 PHP 错误日志:

[05-Dec-2014 14:41:06 Europe/Berlin] PHP Warning:  imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /Applications/MAMP/htdocs/get_docs_from_email.php on line 23
[05-Dec-2014 14:41:06 Europe/Berlin] PHP Notice:  Unknown: Can't open mailbox {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX: invalid remote specification (errflg=2) in Unknown on line 0

有什么帮助吗?

我正在使用的完整代码:

/* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = 'MYEMAIL@gmail.com'; # e.g somebody@gmail.com
$password = 'MYPASSWORD';


/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* get all new emails. If set to 'ALL' instead 
 * of 'NEW' retrieves all the emails, but can be 
 * resource intensive, so the following variable, 
 * $max_emails, puts the limit on the number of emails downloaded.
 * 
 */
$emails = imap_search($inbox,'ALL');

/* useful only if the above search is set to 'ALL' */
$max_emails = 16;

/* if any emails found, iterate through each email */
if($emails) {

    $count = 1;

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) 
    {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);

        /* get mail message */
        $message = imap_fetchbody($inbox,$email_number,2);

        /* get mail structure */
        $structure = imap_fetchstructure($inbox, $email_number);

        $attachments = array();

        /* if any attachments found... */
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }

                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

                    /* 4 = QUOTED-PRINTABLE encoding */
                    if($structure->parts[$i]->encoding == 3) 
                    { 
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    /* 3 = BASE64 encoding */
                    elseif($structure->parts[$i]->encoding == 4) 
                    { 
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        /* iterate through each attachment and save it */
        foreach($attachments as $attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                $filename = $attachment['name'];
                if(empty($filename)) $filename = $attachment['filename'];

                if(empty($filename)) $filename = time() . ".dat";

                /* prefix the email number to the filename in case two emails
                 * have the attachment with the same file name.
                 */
                $fp = fopen("saved_email_docs/" . $email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
            }

        }

        if($count++ >= $max_emails) break;
    }

} 

/* close the connection */
imap_close($inbox);

echo "Done";

编辑:

检查了我的 php.ini,可以看到 imap.so 已启用。我读过 phpinfo 应该显示 --with-imap-ssl 但它现在并不打算改变它。

另请参阅我正在使用 MAMP 和 PHP 5.6.2 的信息

同时使用 telnet 检查连接,我可以正常连接。

最佳答案

为什么要使用/novalidate-cert ? Google 没有有效的证书吗?

也可以尝试使用/debug 标志来获取更多详细信息。

外部问题...您当然已经使用常规邮件客户端连接到此帐户,它确实存在,对吗?我过去在使用 google 时遇到过问题,其中登录名是 blabla@gmail.com,邮件是 blabla@googlemail.com ... 特别是如果帐户有点旧。

关于php - 无法连接 :Can't open mailbox {imap. gmail.com:993/imap/ssl/novalidate-cert}收件箱:无效的远程规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317266/

相关文章:

php - VPS(或物理服务器)上的 SMTP 或 PHP 邮件 - 社交网站

javascript - 将 php 和 python 中的 RegEx 语法转换为 JS

java - 如何在 Java keystore 中导入现有的 X.509 证书和私钥以在 SSL 中使用?

c# - 以编程方式更改 IIS 网站的 SSL 设置

api - Google API Gmail 错误 500 遇到内部错误

caching - Gmail 电子邮件中的图像未显示

c# - Asp.Net 核心 MailKit : The remote certificate is invalid according to the validation proceedure

php - 正则表达式 - 在字符串中查找 8 位数字

php - L5.5 使用 SSH 在共享主机上的存储和公共(public)目录之间创建符号链接(symbolic link)

wordpress - 当我使用 https 时,图像子域在我的 wordpress 网站上不起作用