PHP Classes

how to only read messages once

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  how to only read messages once  >  (Un) Subscribe thread alerts  
Subject:how to only read messages once
Summary:how to read a mailbox and only retrieve new messages
Messages:11
Author:james nahon
Date:2013-02-14 11:37:34
Update:2013-02-21 17:54:25
 
  1 - 10   11 - 11  

  1. how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-14 11:37:34
hi there,

i am using the class to access a pop3 mailbox and then enter the messages into a mysql database.

i cannot get the DeleteMessage() function to work, and the host i am using does not display errors so not sure where to start with that.

is there another way to only read new messages?

  2. Re: how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-14 12:48:45 - In reply to message 1 from james nahon
have read the logs and am getting:

Fatal error: Call to undefined function DeleteMessage()

  3. Re: how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-14 12:50:20 - In reply to message 2 from james nahon
i am getting that error even though i have inlcuded the pop3.php file, and am using other functions from that page ok.

  4. Re: how to only read messages once   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-02-15 00:45:57 - In reply to message 1 from james nahon
Some POP3 servers mark messages as read changing the Status message header, but I cannot guarantee that it will work with your POP3 server.

  5. Re: how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-15 13:53:08 - In reply to message 4 from Manuel Lemos
thanks Manuel.

can you think why i would be getting the error about the DeleteMessage function?

  6. Re: how to only read messages once   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-02-16 00:36:28 - In reply to message 5 from james nahon
You need to call that function using an object of the POP3 class.

$pop3->DeleteMessage();

If you are doing that, maybe the $pop3 variable is not correctly set to an object of the class.

  7. Re: how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-20 12:05:40 - In reply to message 6 from Manuel Lemos
thanks for the help.

is there a way to mark messages as read in the class?

the server is a microsoft exchange server.

  8. Re: how to only read messages once   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-02-20 13:18:31 - In reply to message 7 from james nahon
Not via POP3 because it is a read only protocol. Maybe IMAP allows that somehow.

  9. Re: how to only read messages once   Reply   Report abuse  
Picture of james nahon james nahon - 2013-02-20 13:37:39 - In reply to message 6 from Manuel Lemos
Hey,

I have pasted the whole code that i am using below. The problem is two fold:

1) only the first three messages are processed correctly, after which i get the error below. No matter what the messages are or how many there are, the first three only work.

"MIME message decoding error: could not open the message file to decode pop3://0/4"
"MIME message decoding error: could not open the message file to decode pop3://0/5"

2) when calling $error=$pop3->Close() i get the following error which means the messages are not removed:

Error: Could not get quit command response

Here is the code:

==============================================================
if(($error=$pop3->Open())=="")
{
//echo "<PRE>Connected to the POP3 server &quot;".$pop3->hostname."&quot;.</PRE>\n";
if(($error=$pop3->Login($user,$password,$apop))=="")
{
//echo "<PRE>User &quot;$user&quot; logged in.</PRE>\n";
if(($error=$pop3->Statistics($messages,$size))=="")
{
echo "<PRE>There are $messages messages in the mail box with a total of $size bytes.</PRE>\n";
if($messages>0)
{
$pop3->GetConnectionName($connection_name);
for ($i=1;$i<=$messages;$i++){
print $i;
$message_file='pop3://'.$connection_name.'/'.$i;
$mime=new mime_parser_class;

/*
* Set to 0 for not decoding the message bodies
*/
$mime->decode_bodies = 1;

$parameters=array(
'File'=>$message_file,

/* Read a message from a string instead of a file */
/* 'Data'=>'My message data string', */

/* Save the message body parts to a directory */
/* 'SaveBody'=>'/tmp', */

/* Do not retrieve or save message body parts */
'SkipBody'=>0,
);
$success=$mime->Decode($parameters, $decoded);


if(!$success)
echo '<h2>MIME message decoding error: '.HtmlSpecialChars($mime->error)."</h2>\n";

else
{
//echo '<h2>MIME message decoding successful</h2>'."\n";
//echo '<h2>Message structure</h2>'."\n";
//echo '<pre>';
//var_dump($decoded[0]);
//echo '</pre>';
if($mime->Analyze($decoded[0], $results))
{
//echo '<h2>Message analysis</h2>'."\n";
//echo '<pre>';
//var_dump($results);
$pos = strpos($results['Subject'],"UID:ID");

$id = substr($results['Subject'],$pos+6);
//print "Client ID : <pre>$id</pre>";

$body = mysql_real_escape_string($results['Data']);


$query = "INSERT INTO alerts (client,subject,message,status) VALUES ('$id','$results[Subject]','$body','1')";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
//DeleteMessage($i);
$pop3->DeleteMessage('$i');


//print "Body is: <pre>$results[Data]</pre>";
print "complete <br />";

//echo '</pre>';
}
else
echo 'MIME message analyse error: '.$mime->error."\n";
}
}

}
if($error==""
&& ($error=$pop3->Close())=="")
echo "<PRE>Disconnected from the POP3 server &quot;".$pop3->hostname."&quot;.</PRE>\n";
}
}
}
if($error!="")
echo "<H2>Error: ",HtmlSpecialChars($error),"</H2>";
?>

  10. Re: how to only read messages once   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-02-20 23:43:11 - In reply to message 9 from james nahon
Well the POP3 class is only failing because the server is returning errors for the requested operations. It is hard to tell what are the errors just looking at your code.

Can you set the debug variable to 1 and show the debug output here? For brevity you may truncate the part of the output that shows the message data being retrieved from the server.

 
  1 - 10   11 - 11