无法使用php imap从gmail获取收件箱

nwnhqdif  于 4个月前  发布在  PHP
关注(0)|答案(1)|浏览(64)

你好,伙计们,我试图使用PHP的imap来获取电子邮件,但我得到Network is unreachable错误
这是我的代码

<h1>Gmail Email Inbox using PHP with IMAP</h1>
<?php
    if (! function_exists('imap_open')) {
        echo "IMAP is not configured.";
        exit();
    } else {
        ?>
<div id="listData" class="list-form-container">
    <?php
        
        /* Connecting Gmail server with IMAP */
        $connection = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', 'My correct email address here', 'My correct password here') or die('Cannot connect to Gmail: ' . imap_last_error());
        
        /* Search Emails having the specified keyword in the email subject */
        $emailData = imap_search($connection, 'SUBJECT "Article "');
        
        if (! empty($emailData)) {
            ?>
    <table>
        <?php
            foreach ($emailData as $emailIdent) {
                
                $overview = imap_fetch_overview($connection, $emailIdent, 0);
                $message = imap_fetchbody($connection, $emailIdent, '1.1');
                $messageExcerpt = substr($message, 0, 150);
                $partialMessage = trim(quoted_printable_decode($messageExcerpt)); 
                $date = date("d F, Y", strtotime($overview[0]->date));
                ?>
        <tr>
            <td><span class="column">
                    <?php echo $overview[0]->from; ?>
            </span></td>
            <td class="content-div"><span class="column">
                    <?php echo $overview[0]->subject; ?> - <?php echo $partialMessage; ?>
            </span><span class="date">
                    <?php echo $date; ?>
            </span></td>
        </tr>
        <?php
            } // End foreach
            ?>
    </table>
    <?php
        } // end if
        
        imap_close($connection);
    }
    ?>
</div>

字符串
我应该从我的gmail帐户收到电子邮件,并显示在html中看到的代码上面。请大家让我们表现出一些爱。

k97glaaz

k97glaaz1#

好吧,问题不是来自我的代码,而是最初说不可访问时,它是基于一个封闭的端口993.并在获得证书错误和身份验证失败无效的身份验证.这是在Gmail的结束.我不知道如果打开不太安全的应用程序将解决这个问题,因为我也找不到它.我的意思是Gmail不支持验证与纯文本作为电子邮件和密码.他们使用OAUTH如果你要连接到Gmail,你必须使用它,否则Gmail将无法连接。我在多次尝试后发现。希望这能帮助其他沮丧的用户。如果你需要连接Gmail,请使用OAuth,在我的情况下效果很好。

相关问题