【问题标题】:retrieve gmail inbox through PHP imap function通过 PHP imap 函数检索 gmail 收件箱
【发布时间】:2020-01-26 10:41:52
【问题描述】:

我正在尝试使用此代码从 gmail 收件箱中检索现有电子邮件:

'<?php

$yourEmail = "blablabla@gmail.com";
$yourEmailPassword = "xxxxxxxx";

$mailbox = imap_open("{imap.gmail.com:993/ssl}INBOX", $yourEmail, $yourEmailPassword);
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>'

通过 phpinfo() 我得到:

但我缺少 --with-imap_SSL :

我找不到任何解释如何做的教程。 我还尝试更改差异端口以建立至少一个连接,但没有任何努力。

我给出的错误如下:

PHP Warning:  imap_open(): Couldn't open stream {imap.gmail.com:993/ssl}INBOX in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 6, referer: http://localhost/otika-bootstrap-admin-template/

PHP Warning:  imap_search() expects parameter 1 to be resource, bool given in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 7, referer: http://localhost/otika-bootstrap-admin-template/

PHP Warning:  imap_headerinfo() expects parameter 1 to be resource, bool given in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 8, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Trying to get property 'subject' of non-object in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 9, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Trying to get property 'fromaddress' of non-object in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 10, referer: http://localhost/otika-bootstrap-admin-template/

PHP Warning:  imap_setflag_full() expects parameter 1 to be resource, bool given in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 11, referer: http://localhost/otika-bootstrap-admin-template/

PHP Warning:  imap_close() expects parameter 1 to be resource, bool given in G:\\xampp\\htdocs\\otika-bootstrap-admin-template\\imap.php on line 12, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0, referer: http://localhost/otika-bootstrap-admin-template/

PHP Notice:  Unknown: Too many login failures (errflg=2) in Unknown on line 0, referer: http://localhost/otika-bootstrap-admin-template/

在这个领域有 exp 的人可以告诉我如何重新编译 php 以包含 --with-imap_sll,因为我认为这可能是问题所在?

我也启用了

imap

在 gmail 上。 我尝试了所有与

相关的主题

imap

过去 2 天内在 stackoverflow 或其他任何地方。

感谢阅读。

【问题讨论】:

    标签: php email gmail imap


    【解决方案1】:
    <?php
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'xxxxx@gmail.com';
    $password = 'xxxxx';
    
    /* 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 SINCE "29 December 2019"');
    
    /* useful only if the above search is set to 'ALL' */
    $max_emails = 5;
    
    /* 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);
    ?>
    

    这对我有用,不知道为什么,但它在做这项工作。如果我也将$email 设置为

    全部

    由于电子邮件数量巨大,它不会起作用。我决定展示ALL SINCE x Date。我也不需要为 --with-imap-ssl 重新编译 PHP。

    【讨论】:

    • 谢谢毛罗。被重复的电子邮件和大量的坏东西弄疯了。没有想出一种只选择收件箱的方法,gmail 使用不同的类别....'ALL SINCE "29 December 2019"!
    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2010-11-26
    • 2019-04-29
    相关资源
    最近更新 更多