I guess I should brush up on what IMAP actually does one of these millennia... Too many FLEAs...
I can suggest one improvement:
$b = ereg_replace("@mydomain.com","",$b);
is in a sense incorrect: since . will match any character, the above expression will happily turn "@mydomainacom.com" into ".com". Okay, a rather contrived example, but still, the principle's there.
Fortunately, while the . could just be escaped (i.e., use .), the fact that there isn't anything particular regexy about that expression and is actually just a string means that
$b = str_replace("@mydomain.com","",$b);
would see a marked performance improvement.
Similarly, using strpos instead of ereg to detect plain substrings in longer strings would also be an efficiency gain.
The only other tiddly little change I can see would be turning
for($i = 1; $i < $num_emails+1; $i++)
into
for($i = 1; $i <= $num_emails; $i++)
....saves one addition per iteration.
(And is it "DELETE * FROM" or just "DELETE FROM"?)
All pretty minor, but hey - I get paid for being pedantic.
Phew, and I managed to get through all that without saying "use preg_match() instead!" ..... damn.