is there a way to check if the email server is working via PHP?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 offHave 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 returnsfalse, there is something fundamentally wrong with your mail setup. There are many, many eventualities that will not returnfalsehere though, so it’s not really a thorough check.