无法连接:无法打开邮箱{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX:无效的远程规范

时间:2014-12-05 13:43:37

标签: 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检查连接我可以正常连接。

1 个答案:

答案 0 :(得分:0)

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

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

外面的问题......你当然使用常规邮件客户端bevor连接到这个帐户,它确实存在,对吧?我过去遇到了谷歌的问题,其中登录的是blabla@gmail.com,邮件是blabla@googlemail.com ...特别是如果帐户有点旧。