GuRu GuRu - 4 months ago 21
PHP Question

Getting all mails from Gmail using PHP

My PHP Code:

<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'my gmail id';
$password = 'my gmail 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;
}

/* close the connection */
imap_close($inbox);
?>


Problem is that first I get error like that Cannot connect to Gmail: Too many login failures and after I remove
/SSL
from
$hostname
that gives me error like Cannot connect to Gmail: [CLOSED] IMAP connection broken (server response).

I do all the thing related for connection like enable pop3 and Imap, Unlock Google account and also Turn on less secure apps.

But, I can't find out what actual problem?

Answer

That's not work because of i used ALL in imap_search so, that get lots of mail and unable to pass huge mail. After i changed code as per the search criteria for giving the limit and that's work fine

    ALL - return all messages matching the rest of the criteria
    ANSWERED - match messages with the \\ANSWERED flag set
    BCC "string" - match messages with "string" in the Bcc: field
    BEFORE "date" - match messages with Date: before "date"
    BODY "string" - match messages with "string" in the body of the message
    CC "string" - match messages with "string" in the Cc: field
    DELETED - match deleted messages
    FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
    FROM "string" - match messages with "string" in the From: field
    KEYWORD "string" - match messages with "string" as a keyword
    NEW - match new messages
    OLD - match old messages
    ON "date" - match messages with Date: matching "date"
    RECENT - match messages with the \\RECENT flag set
    SEEN - match messages that have been read (the \\SEEN flag is set)
    SINCE "date" - match messages with Date: after "date"
    SUBJECT "string" - match messages with "string" in the Subject:
    TEXT "string" - match messages with text "string"
    TO "string" - match messages with "string" in the To:
    UNANSWERED - match messages that have not been answered
    UNDELETED - match messages that are not deleted
    UNFLAGGED - match messages that are not flagged
    UNKEYWORD "string" - match messages that do not have the keyword "string"
    UNSEEN - match messages which have not been read yet

PHP Code

$sinceDate = "31 December 2015";
$beforeDate = "06 January 2016"; 
$emails = imap_search($inbox,'SINCE "'.$sinceDate.'" BEFORE"'.$beforeDate.'"' );

After i work on downloading attachment in directory using IMAP and its well work answer on Downloading attachments to directory with IMAP in PHP, randomly works

Comments