连接gmail通过PHP imap?本地颁发者证书错误


connect gmail through php imap? local issuer certificate error

我需要使用PHP通过IMAP连接到Gmail帐户。我有一些工作代码,但是当尝试建立连接时,我收到以下错误:

警告:imap_open() [function。无法打开流{imap.gmail.com:993/ssl}[Gmail]/所有邮件在/home/demoosize/public_html/goqule/email.php在第31行
无法连接到Gmail: imap.gmail.com证书失败:无法获得本地发行者证书:/C=US/O=Google Inc/CN=Google Internet Authority

我的代码在

下面
<?php
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/ssl}[Gmail]/All Mail';
    $username = 'hidden';
    $password = 'hidden';
    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox,'ALL');
    /* if emails are returned, cycle through each... */
    if($emails) {
      /* begin output var */
      $output = '';
      /* 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);
        $message = imap_fetchbody($inbox,$email_number,2);
        /* output the email header information */
        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';
        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
      }
      echo $output;
    } 
    /* close the connection */
    imap_close($inbox);
    ?>

这似乎是一个证书问题。

我没有告诉你证书问题是什么,也没有告诉你如何解决。

然而,你可以让你的gmail帐户打开攻击忽略这个问题禁用证书验证:

$hostname = '{imap.gmail.com:993/ssl/novalidate-cert}[Gmail]/All Mail';

无法连接到Gmail: imap.gmail.com证书失败:无法获得本地发行者证书:/C=US/O=Google Inc/CN=Google Internet Authority

执行代码的系统缺少该证书。关于这个问题,你提供的信息没有什么可说的。如何安装证书以及如何获取所需的证书,请参考您所使用的操作系统的系统文档。

参见:

  • 在PHP中使用IMAP证书错误