Page 1 of 1

Simple IMAP Help

Posted: Thu Mar 01, 2007 8:37 pm
by itstooloud
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Beginner problem.

I have a little piece of code that will search a mailbox for the string "hello" within the subject of unread messages. It will display which message number the string lies in, if any. My problem lies in the "number of messages found" and the "match found in."

If 14 messages are found with the string it will display each message # that it is found in as well as a total of 14.
If 0 messages are found with the string it will display "0 Match found in number:" This is fine because I will not need this line, it's just for debugging purposes.
The problem is that the "Number of Messages Found:" will display 1, which would be incorrect.

Any suggestions? I've looked around quite a bit and can't find the answer. Everything I seem to do that corrects the incorrect count of 1 will subtract 1 from any answer that is correct. Thanks for your time.
[syntax="php"]
//open imap stream
$mail = imap_open("{pop.server.com}INBOX","user","password");

// search only in the subject of unread messages for the string 'hello'
$search_mail = imap_search($mail, "UNSEEN SUBJECT hello");
for ($i=0; $i<count($search_mail); $i++) {
   echo "$i Match found in number: $search_mail[$i]<BR>\n";
}
echo "<br /><b>Number of Messages Found: $i</b><br /><br />";

imap_close($mail);

hawleyjr | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Mar 02, 2007 8:24 am
by volka
try

Code: Select all

echo '<br /><b>Number of Messages Found: ', count($search_mail), '</b><br /><br />';

Posted: Fri Mar 02, 2007 8:46 am
by itstooloud
Hey, thanks for the reply. I tried that and it still wouldn't return what I needed it to. I managed to figure out a way around it. I added an if statement belolw which would set the variable $max to zero if the search returned an empty array.

Code: Select all

//open imap stream 
$mail = imap_open("{pop.server.com}INBOX","user","password"); 

// search only in the subject of unread messages for the string 'hello' 
$search_mail = imap_search($mail, "UNSEEN SUBJECT hello"); 
for ($i=0; $i<count($search_mail); $i++) { 
   echo "$i Match found in number: $search_mail[$i]<BR>\n"; 
} 

if ( $search_mail == "" ) {
    $i = 0;
}

echo "<br /><b>Number of Messages Found: $i</b><br /><br />"; 

imap_close($mail);

Posted: Fri Mar 02, 2007 8:55 am
by volka
ah, imap_search doesn't return an empty array if no messages match but false.

Code: Select all

$mail = imap_open('{pop.server.com}INBOX','user','password');
$search_mail = imap_search($mail, "UNSEEN SUBJECT hello");

if ( false!==$search_mail ) {
  for ($i=0; $i<count($search_mail); $i++) {
    echo "$i Match found in number: $search_mail[$i]<BR>\n";
  }
}
else {
  $i = 0;
}
imap_close($mail); 

echo "<br /><b>Number of Messages Found: $i</b><br /><br />";

Posted: Fri Mar 02, 2007 8:57 am
by itstooloud
When checking the mail, it seemed to work perfect. I'll try your suggestion and if it works, I 'll use it. I'd rather use the right way that works as opposed to the wrong way that works.



Thanks.

Switching User

Posted: Sat Mar 03, 2007 1:39 pm
by itstooloud
I have another question regarding IMAP connections that I can't seem to find anywhere. Is there a way to keep the connection alive and switch users on the server? It seems really slow loggin in as 7 different accounts on by one. If I don't use imap_close($mailserver); after checking each account would it be faster switching accounts?

Also, how would you search a string in the subject of a message? Say I have an e-mail with the subject "Hello World".

Code: Select all

// search for replies
$search_replies = imap_search($mbox, "UNSEEN SUBJECT hello");
if ( false!==$search_replies ) {
	for ($replies=0; $replies<count($search_replies); $replies++) {
	}
}
else {                       // return zero if search is false
    $replies = 0;
}
I use the above code to search for e-mails with "hello" in them. This will make $replies = 1.

Code: Select all

// search for replies
$search_replies = imap_search($mbox, "UNSEEN SUBJECT hello world");
if ( false!==$search_replies ) {
	for ($replies=0; $replies<count($search_replies); $replies++) {
	}
}
else {                       // return zero if search is false
    $replies = 0;
}
I use the above code to search for e-mails with "hello world" in them. This code, however, will make $replies = 0. What am I missing to allow it to search strings of text?