• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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' => 'xxxx@gmail.com',
    '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' => 'xxxx@gmail.com',
    '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

      • 2
    • You say it hangs up with doing nothing? As long as CPU isn't at 100%, it probably hangs in some syscall, … try a strace then and look what the last syscall(s) are.
      • 1
    • Try settings timeout for fetching email with imap_timeout - your echo can accidentially make this timeout for second script.
      • 2
    • Can you still reproduce the error ? If so what php version are you using and what shell are you on while executing the scripts ?
      • 1
    • How are you connecting to the machine actually running the script? If it's remote, it could be that the inactivity over stdio (the terminal connection), whilst it fetches and downloads the message on the remote computer, exceeds your connection timeout. Given how long it takes to get to 'over' in code2, can you connect a separate window to the machine running code1 and see if the process is actually, still running, after the same amount of time. Similarly check the script time limit, in php_cli

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' => 'xxxx@gmail.com',
    '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.

  • 0
Reply Report
      • 1
    • all the code ,my code1 code2 and AddWeb Solution Pvt Ltd'code can run with putty to connect my vps ,and get all the same result both on command line mode and on browser mode. local os is windows, server os is debian7.
    • local os is debian7, server os is debian7, my code1 run on browser mode ,get the wanted result.when my code1 run on command line mode ,can't stop after download all the emails .
      • 2
    • So, i draw the conclusion that it is a problem of hibernation on debian7 ,the programs run so long time ,nothing output cause the debian console sleep until all the emails downloaded ,it can't wake .
<?php
$mailbox = array(
    'mailbox'  => '{imap.gmail.com:993/imap/ssl}INBOX',
    'username' => 'xxxx@gmail.com',
    '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.

  • -1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions