tags:

views:

59

answers:

1

Hey all, is there a way to check if the email server is working via PHP?

Thanks.

+4  A: 

I'd say the only really, really reliable way of testing whether the mail server works is to actually send an E-Mail.

  • Use mail() to send out an E-Mail to an external address (i.e. one that is not hosted on the local server) containing a unique identifier in the subject. The external mailbox belongs to you and must have spam filtering turned off

  • Have a PHP script poll the recipient mail box for new messages, e.g. using one of the libraries Gordon provides in the comments

  • Depending on server, it could take 5-10 minutes' time because of possible delayed delivery (Greylisting)

  • Once the E-Mail has successfully arrived, report success

  • If the E-Mail hasn't arrived within the time frame specified by you, report failure

if you have a sender/recipient server relationship that you know to deliver E-Mails instantly, you might get around the "wait for 5-10 minutes" part and just sleep() 10-20 seconds before checking the mail box. Otherwise, you may have to set up a cron job if you can't run a PHP script for that long, which complicates things.

That said, a much simpler but much more basic test for whether the mail server works is sending the test E-Mail using mail(), and checking its return value. If it returns false, there is something fundamentally wrong with your mail setup. There are many, many eventualities that will not return false here though, so it's not really a thorough check.

Pekka