通过PHP imap函数检索gmail收件箱

问题描述 投票:0回答:1

im试图从gmail收件箱中检索具有以下代码的现有电子邮件:

'<?php

$yourEmail = "[email protected]";
$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()我得到了:enter image description here

但缺少--with-imap_SSL:enter image description here

我找不到任何说明方法的教程。我也尝试更改差异端口以建立至少一个连接,但没有进行任何操作。

给出的错误消息如下:

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/

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

我也启用了

imap

在gmail。我尝试了所有与

相关的主题

imap

在最近2天的stackoverflow上或其他任何地方。

感谢阅读。

php email gmail imap
1个回答
0
投票
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$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。

© www.soinside.com 2019 - 2024. All rights reserved.