Home » Php » php – why the program can't stop after downloaded all the emails?

php – why the program can't stop after downloaded all the emails?

Posted by: admin April 23, 2020 Leave a comment

Questions:

There are 2465 emails in my gmail,why the program can’t stop after download all the emails? All code1 and code2 are run in command line mode.
code1:

<?php
$mailbox = array(
    'mailbox'  => '{imap.gmail.com:993/imap/ssl}INBOX',
    'username' => '[email protected]',
    'password' => 'yyyy'
);

$stream = imap_open($mailbox['mailbox'], $mailbox['username'], $mailbox['password'])
    or die('Cannot connect to mailbox: ' . imap_last_error());
$emails = imap_search($stream,"ALL");
$nums=imap_num_msg($stream);
echo $nums;
foreach($emails as $email_id) {

            $mime = imap_fetchbody($stream, $email_id, "");
            file_put_contents("/tmp/" . "email_{$email_id}.eml", $mime);
        }

imap_close($stream);
echo "over";
?>

For code1 :
1.can download all the emails.
2.output 2465 on the console
3.no over output on the console.
4.the program can’t stop ,it seems to run forever.

code2:

<?php
$mailbox = array(
    'mailbox'  => '{imap.gmail.com:993/imap/ssl}INBOX',
    'username' => '[email protected]',
    'password' => 'yyyy'
);

$stream = imap_open($mailbox['mailbox'], $mailbox['username'], $mailbox['password'])
    or die('Cannot connect to mailbox: ' . imap_last_error());
$emails = imap_search($stream,"ALL");
$nums=imap_num_msg($stream);
echo $nums;
foreach($emails as $email_id) {
            echo  $email_id.PHP_EOL;
            $mime = imap_fetchbody($stream, $email_id, "");
            file_put_contents("/tmp/" . "email_{$email_id}.eml", $mime);
        }

imap_close($stream);
echo "over";
?>

For code2:
1.can download all the emails.
2.output 2465 on the console .
3.over output on the console.
4.the program stop after download all the emails.

There is one line echo $email_id.PHP_EOL; in code2 more than code1,other codes are same.
All code1 and code2 are run in command line mode.
Who can explain it ?

enter image description here

How to&Answers:

Try

print imap_last_error();

Before and after imap_close to know more about what causes your problem

Answer:

Check imap_fetchbody() function, this function allows you to set download limit and with this you can stop after all mail downloads. Check below code, I made some changes into your code that will stop download process after all mails are download. Please check once.

<?php
$mailbox = array(
    'mailbox'  => '{imap.gmail.com:993/imap/ssl}INBOX',
    'username' => '[email protected]',
    'password' => 'yyyy'
);

$stream = imap_open($mailbox['mailbox'], $mailbox['username'], $mailbox['password'])
    or die('Cannot connect to mailbox: ' . imap_last_error());

//Check all mails...
$imap_obj = imap_check($stream);

// Fetch an overview for all messages in INBOX...
$nums=imap_num_msg($stream);
$emails = imap_fetch_overview($stream,"1:{$imap_obj->Nmsgs}",0);

echo $nums;
//Loop until mail data...
foreach($emails as $objEmail_data) {
    $mime = imap_fetchbody($stream, $objEmail_data->msgno, "");
    file_put_contents("/tmp/" . "email_{$objEmail_data->msgno}.eml", $mime);
}

imap_close($stream);
echo "over";
?>

And see the results!

I replaced imap_search() with imap_fetch_overview() and made some relevant changes.

Let me know if any further help required.

Answer:

<?php
$mailbox = array(
    'mailbox'  => '{imap.gmail.com:993/imap/ssl}INBOX',
    'username' => '[email protected]',
    'password' => 'yyyy'
);

$stream = imap_open($mailbox['mailbox'], $mailbox['username'], $mailbox['password'])
    or die('Cannot connect to mailbox: ' . imap_last_error());
$emails = imap_search($stream,"ALL");
$nums=imap_num_msg($stream);
//echo $nums; // removed this
foreach($emails as $email_id) {

            $mime = imap_fetchbody($stream, $email_id, "");
            file_put_contents("/tmp/" . "email_{$email_id}.eml", $mime);
        }
unset($emails); // added this
imap_close($stream);
die ("over"); // changed this
?>

I have reset the emails variables.