получить входящие Gmail через функцию PHP imap - PullRequest
0 голосов
/ 26 января 2020

я пытаюсь получить из почтового ящика 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 () я получаю: enter image description here

НО им отсутствует --with-imap_SSL: enter image description here

, в котором я не смог найти учебник, объясняющий, как это сделать. Я также попытался изменить diff port, чтобы установить sh хотя бы соединение, но без усилий.

Ошибка, которую я привел, выглядит следующим образом:

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 дня.

Спасибо за чтение.

1 Ответ

0 голосов
/ 26 января 2020
<?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

, это не сработает, так как количество писем огромно. я решил показать ALL SINCE x Date. Мне также не нужно было перекомпилировать PHP для --with-imap-ssl.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...