Jump to content

[SOLVED] PHP Warning


ballouta

Recommended Posts

Hello all

 

I have some questions, i have very beginner with PHP.

First i would know if the warning is a bug or it is an error from the code i am working on

When I get a 'Warning' Message, does this mean that my code runs correctly but its better to fix this problem?

 

I had three PHP codes that work on IMAP emails, I integrated the mand I got this PHP code.

Note that this code do the following:

 

1. read number of messages in 'inbox'

2. copy attachment from these messages to a directory names 'malek'

3. at the end the code deletes then purges the messages.

 

I thought it might be better to begin deleting messages from the end, i was wondering if i delete this first message, does the second message's index changes to 1 and stays in the inbox and not deleted?

 

<?php

$SaveFileDirectory = "/home/cpanel_user/public_html/malek/";


function WriteToFile($file,$contents)	{
// IF the contents is empty lets just make it one space
if ($contents=='' || empty($contents))
	$contents = " ";

// $file is the full path to the file
$fh = fopen($file, "w") or die("Could not open file!");
fwrite($fh, $contents) or die("Could not write to file");
fclose($fh);
}


//check for new messages
$mailbox = imap_open("{localhost:143/notls}INBOX", "[email protected]", "mypass")
or die("Can't connect: " . imap_last_error());;

// Check messages
$check = imap_check($mailbox);

// viewing number of messages in inbox
$chec = imap_mailboxmsginfo($mailbox);
echo "Number of Messages: " . $chec->Nmsgs . "<br />\n";
//$cont=$chec->Nmsgs;



$headers = @imap_headers($mailbox) or die("Couldn't get emails");
$numEmails = sizeof($headers); echo "You have $numEmails in your mailbox <br/>";


for($j = $numEmails; $j >=1; $j--) 
{ 
$mailHeader = @imap_headerinfo($mailbox, $j);
$from = $mailHeader->fromaddress; 
$subject = strip_tags($mailHeader->subject); 
$date = $mailHeader->date;
//echo "Email from $from, subject $subject, date $date<br>"; 



if ($check->Nmsgs=='0')	{
echo "There are no messages that need to be downloaded"; 	
}	else	{

$emailIndex = $j;
echo "Processing email " . $emailIndex . "<br>";
$header = imap_header($mailbox, $emailIndex);
print("<PRE>");
print("Header Date : " . $date . "<BR>");
print("Header To : " . $header->to) . "<BR>";
print("Header From : " . $from . "<BR>");
print("Header cc : " . $header->cc . "<BR>");
print("Header ReplyTo : " . $from . "<BR>");
print("Header Subject : " . $subject . "<BR></PRE>");

/* Lets find the email body */
$struct = imap_fetchstructure($mailbox, $emailIndex);
$parts = $struct->parts;
$i = 0;


if (!$parts) { /* Simple message, only 1 piece */
  $attachment = array(); /* No attachments */
  $Email_Comments = imap_body($mailbox, $emailIndex);
} else { /* Complicated message, multiple parts */

  $endwhile = false;

  $stack = array(); /* Stack while parsing message */
  $Email_Comments = "";    /* Content of message */
  $attachment = array(); /* Attachments */

  while (!$endwhile) {
    if (!$parts[$i]) {
      if (count($stack) > 0) {
        $parts = $stack[count($stack)-1]["p"];
        $i     = $stack[count($stack)-1]["i"] + 1;
        array_pop($stack);
      } else {
        $endwhile = true;
      }
    }

    if (!$endwhile) {
      /* Create message part first (example '1.2.3') */
      $partstring = "";
      foreach ($stack as $s) {
        $partstring .= ($s["i"]+1) . ".";
      }
      $partstring .= ($i+1);
   
      if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
        $attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
                              "filedata" => imap_fetchbody($mailbox, $emailIndex, $partstring));
      } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
        $Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring);
		echo "Email Body: $Email_Comments <br/><br/>";
		$daouk=$Email_Comments; // my code
		$emailIndex +=1;
      }
    }

    if ($parts[$i]->parts) {
      $stack[] = array("p" => $parts, "i" => $i);
      $parts = $parts[$i]->parts;
      $i = 0;
    } else {
      $i++;
    }
  } /* while */
} /* complicated message */
	foreach ($attachment as $attach)	{
		WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata']));
		echo "Attachment found: " . $attach['filename'] . "<br><br><br><br>";
	}
} // end else

imap_delete($mailbox, $j);
imap_expunge($mailbox);

} // end for

 

I am getting this warning, How do I fix it? (live: http://www.motif-services.com/surpass.php)

Warning: imap_fetchbody() [function.imap-fetchbody]: Bad message number in /home/motifser/public_html/surpass.php on line 99

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/107108-solved-php-warning/
Share on other sites

When I get a 'Warning' Message, does this mean that my code runs correctly but its better to fix this problem?

 

correct. it is possible to turn down error reporting to not display warnings, but it is better to fix the problem.

 

i don't know much about imap. i would expect a mail queue to act like array, where if the first element is removed, the element behind it becomes the first element. so i would expect the message index to act similarly, as you described.

Link to comment
https://forums.phpfreaks.com/topic/107108-solved-php-warning/#findComment-549119
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.