I have the following issue. With one webserver the email (using the mail function) is sent almost immediately to every mailbox available. However on another server it takes several hours to be received.
At the same time on the second webserver, there are installed scripts (i.e. phpFox), that send their email immediately.
I have been looking on the Internet for a solution, however non of them seems to be working.
phpFox is probably sending the emails directly via SMTP and bypassing the local queues on that server.
The php mail function will send it locally through postfix, sendmail, qmail, exim etc and you are at the mercy of the queue and relay configuration of that application doing the actual sending. mail() requires a functioning MTA on that server to do the dirty work for it. phpMailer, phpFox, Pear::Mail can all send via SMTP bypassing the need for an MTA on the sending server as the scripts themselves act as send only MTA's.
Even if you get the MTA on the server working optimally (if you have control over it), you could still be at the mercy of spam and virus scanners, and even network routing as well. There are a lot of variables to consider, and only one of them involves you actually coding something. 99.99% of them are infrastructure related.