[SOLVED] IMAP functions don't seem to work when in my loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

[SOLVED] IMAP functions don't seem to work when in my loop

Post by impulse() »

I have wrote a script that I will eventually CRON that will keep an eye on a mailbox for me. It will go through each message in a mailbox and insert all the contents including attachments into a database.

The problem I'm having at the moment is that on the first loop it picks up the headers using imap_header and the structure using imap_fetchstructure but once it completes its first loop it will no longer retrieve the headers or the structure. I am passing in the correct mailbox message numbers from the loop and I have tried assigning the headers and body structure manually to different variables and it works fine also.

For example:

This works (manual)

Code: Select all

$header = imap_header($mailbox, 1);
print_R($header);
$header = imap_header($mailbox, 2);
print_R($header);
$header = imap_header($mailbox, 3);
print_r($header);
This next part is a bit spammy, sorry, but I think all of the code is important for you to see:

Code: Select all

$months = array(1=>"Jan", 2=>"Feb", 3=>"Mar", 4=>"Apr", 5=>"May", 6=>"Jun", 7=>"Jul",
                8=>"Aug", 9=>"Sep", 10=>"Oct", 11=>"Nov", 12=>"Dec");

$mailbox = imap_open("{x:110/pop3}INBOX", "x", "x");
$numMessages = imap_num_msg($mailbox);

$header = imap_header($mailbox, 1);
print_R($header);
$header = imap_header($mailbox, 2);
print_R($header);
$header = imap_header($mailbox, 3);
print_r($header);

for($i = 1; $i <= $numMessages; $i++) {

  # Get headers for e-mail.
  $header = imap_header($mailbox, $i);

  # Fetch message structure.
  $struct = imap_fetchstructure($mailbox, $i);

  # Message date.
  $messageDate[$i] = $header->date;

  # Count amount of 'to' headers.
  $toCount = count($header->to);
  for($z = 0; $z < $toCount; $z++) {
    $to[] = array(0 => $i,
                  1 => $header->to[$z]->mailbox,
                  2 => $header->to[$z]->host);
  }

  # Count amount of 'cc' headers.
  $ccCount = count($header->ccaddress);
  for($z = 0; $z < $ccCount; $z++) {
    $cc[] = array(0 => $i,
                  1 => $header->ccaddress[$z]->mailbox,
                  2 => $header->ccaddress[$z]->host);
  }

  # Count amount of 'bcc' headers.
  $bccCount = count($header->bccaddress);
  for($z = 0; $z < $bccCount; $z++) {
    $bcc[] = array(0 => $i,
                   1 => $header->bccaddress[$z]->mailbox,
                   2 => $header->bccaddress[$z]->host);
  }
# Check if message has an attachment.
  if (isset($struct->parts[1]->dparameters[0]->value)) {
    $fileName = $struct->parts[1]->dparameters[0]->value;
    $body     = imap_fetchbody($mailbox, $i, 2);
    $file     = imap_base64($body);
    $fh       = fopen("/home/steve/www/imap/attachments/$fileName", "w");
    fputs($fh, $file);
    fclose($fh);

    $fileInsert = file_get_contents("/home/steve/www/imap/attachments/$fileName");

    mysql_query("INSERT INTO imap_attachments (messID, attachment)
                 VALUES ('$i', '".mysql_escape_string($fileInsert)."')");
  }

  # Insert message date
  $cutDate   = explode(" ", $messageDate[$i]);
  $cutTime   = explode(":", $cutDate[4]);
  $timeStamp = mktime($cutTime[0], $cutTime[1], $cutTime[2], $months[$cutDate[2]], $cutDate[1], $cutDate[3]);
  mysql_query("INSERT INTO imap_messages (date)
               VALUES ('$timeStamp')");

  # Insert 'to' headers.
  foreach($to as $output) {

    list($messID, $mailbox, $host) = $output;
    mysql_query("INSERT INTO imap_to (messID, to)
                 VALUES ('$i', '$mailbox@$host')");

  }

  # Insert 'cc' headers.
  foreach($cc as $output) {

    list($messID, $mailbox, $host) = $output;
    mysql_query("INSERT INTO imap_cc (messID, to)
                 VALUES ('$i', '$mailbox@$host')");

  }

  # Insert 'bcc' headers.
  foreach($bcc as $output) {

    list($messID, $mailbox, $host) = $output;
    mysql_query("INSERT INTO imap_bcc (messID, to)
                 VALUES ('$i', '$mailbox@$host')");

  }

}

But I'm receiving these errors, which indicate that the script isn't picking up the headers or body structure after the first loop:
Warning: Invalid argument supplied for foreach() in /usr/home/steve/www/imap/index.php on line 86

Warning: Invalid argument supplied for foreach() in /usr/home/steve/www/imap/index.php on line 95

Warning: imap_header(): supplied argument is not a valid imap resource in /usr/home/steve/www/imap/index.php on line 22

Warning: imap_fetchstructure(): supplied argument is not a valid imap resource in /usr/home/steve/www/imap/index.php on line 25

Warning: mktime() expects parameter 1 to be long, string given in /usr/home/steve/www/imap/index.php on line 72
Hopefully somebody has come across this before.
Last edited by impulse() on Tue Jul 24, 2007 5:05 am, edited 1 time in total.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I've just placed the imap_open function within the loop and it seems to have sorted the problem.

I can't understand why though. I wasn't closing the imap connection within the loop anywhere.

Regards,
Post Reply