【问题标题】:Unable to connect:Can't open mailbox {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX: invalid remote specification无法连接:无法打开邮箱 {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX:远程规范无效
【发布时间】:2015-02-03 16:54:24
【问题描述】:

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

尝试保存来自 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 检查连接我可以正常连接。

【问题讨论】:

    标签: php ssl gmail imap gmail-imap


    【解决方案1】:

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

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

    外面的一个问题...您当然已经使用普通邮件客户端连接到这个帐户,它确实存在,对吗?过去我在使用 google 时遇到过问题,登录地址是 blabla@gmail.com,邮件地址是 blabla@googlemail.com ......尤其是如果帐户有点旧。

    【讨论】:

    • 我已经为主机名尝试了一切,我读到 novalidate cert 适用于某些人等。是的,只是将它连接到我的 iPhone。我认为问题在于 PHP,显然默认情况下 MAMP 没有启用 IMAP SSL。
    • 我刚刚意识到您正在使用 MAMP !哦,好吧...查看他们的论坛... 10% 的帖子正在重新定义 imap-ssl 不包括在内。由于我没有运行 MAMP,我无法确认这一点,但如果您使用 Google 搜索,有一些关于自己使用 imap-ssl 编译 MAMP 的详细信息。很抱歉没有更好的消息。
    猜你喜欢
    • 2014-11-01
    • 2021-03-27
    • 2019-05-11
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2020-04-07
    • 2011-09-18
    相关资源
    最近更新 更多