【问题标题】:How to fetch Gmail on PHP IMAP?如何在 PHP IMAP 上获取 Gmail?
【发布时间】:2014-10-30 03:07:47
【问题描述】:

我试试这个主机:

{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl}INBOX
{imap.gmail.com:993/ssl/novalidate-cert}Current Batch
{imap.gmail.com:993/ssl/novalidate-cert}
{imap.gmail.com:995/ssl/serivce=pop3}INBOX

他们都没有工作。什么是 Gmail 主机或 IMAP 设置?

谢谢!

【问题讨论】:

  • 您是否检查过您的主机上的端口 993 没有被防火墙关闭?

标签: php gmail imap


【解决方案1】:

PHP IMAP 和连接 gmail 存在几个问题。

首先确保您已在您的 gmail 设置中启用 IMAP(默认为关闭!)。

PHP 上的第二个 SSL IMAP 存在多个版本的问题。

要让它工作,请选择:{imap.gmail.com:993/ssl/novalidate-cert}

进入您的 Google 帐户(这次不是 gmail),进入安全性并允许通过不太安全的方法进行访问(允许不太安全的应用)。

然后就可以了。 希望有一天能修复

【讨论】:

    【解决方案2】:

    这里是从 gmail 连接和获取电子邮件的完整代码示例:

    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'gmail_email_address';
    $password = 'password';
    /* 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;
    }
    imap_close($inbox);
    
    • 输入正确的电子邮件 ID 和密码。
    • 你可能想要替换

      $emails = imap_search($inbox,'ALL');

    用这样的方法来缩小结果

    $emails = imap_search($inbox,'SINCE "19 October 2018"');
    

    否则您可能会收到超时错误。

    【讨论】:

      【解决方案3】:

      设置如下:

      $mail = new Imap([
            'host'  => 'imap.gmail.com',
            'user'  => 'username@gmail.com',
            'password' => 'password',
            'port'  => 993,
            'ssl'   => 'SSL',
      ]);
      

      然后转到https://myaccount.google.com/security#connectedapps 并选中“允许安全性较低的应用程序”。

      【讨论】:

        【解决方案4】:

        imap.gmail.com:993/imap/ssl}INBOX 应该是正确的。你是如何在你的 php 代码中使用它的?您只是想收到您的电子邮件吗?因为它看起来像这样:

        /* connect to gmail */
        $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
        $username = 'youremail@gmail.com';
        $password = 'password';
        
        /* try to connect */
        $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
        

        【讨论】:

        • 我的代码是: 答:连接超时。
        • 在 echo $num_msgs 后关闭连接有帮助吗?
        • 没有切中要害。 PHP 退出连接的 $inbox 行超时。不会打印 $num_msgs。
        • 如果您收到类似“请通过您的网络浏览器登录:”的错误,请尝试 2​​ setp 验证和应用程序特定密码(由 abhishek gupta stackoverflow.com/questions/35377560/… 回答)
        • 男人!你拯救了一个可怜的灵魂。
        猜你喜欢
        • 1970-01-01
        • 2013-02-06
        • 1970-01-01
        • 2015-01-17
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        相关资源
        最近更新 更多