10

I'm writing a fairly simple register php script that uses PHP's built in mail() function to email the user an activation link.

The problem is that I can catch the normal errors such as email formatting but once it fires off to the server and say a user has put in an email address that fails, I don't know how to catch this error and tell the user whats happened.

For example at the moment I get this:

Warning: mail() [function.mail]: SMTP server response: 554 : Recipient address rejected: Relay access denied in ** on line 70

Any ideas what I could do about errors like this? I'm aware of using the @ symbol to suppress the error but I kinda of want to do more than that and handle the issue.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Cliftwalker
  • 369
  • 1
  • 5
  • 17

4 Answers4

21

Use the boolean result to detect an error:

$success = @mail(...);

Then you want to find out which internal error caused the problem, so use:

$error = error_get_last();
preg_match("/\d+/", $error["message"], $error);
switch ($error[0]) {
    case 554:
        ...
    default:
        ...

Note that this works with php 5.2 onward only.

There is no way to verify delivery or see transport error mails with PHP. You would need a pop3 polling handler for that.

mario
  • 144,265
  • 20
  • 237
  • 291
1

A trivial error which I suffered from, was simply a lack of 'sendmail' in my system. Eventually, I have installed exim4 and configured it - and then php's mail(...) worked fine.

See also:

Yotam
  • 808
  • 8
  • 10
0

You want to catch as much as you can before you send off the mail, sanitize your inputs to make sure it really is what you expect it to be

You can use filter_var to check that the email address really is an address, check that integers falls within your allowed range, etc.

Set the mail header from address to a place where you can check for failed deliveries:

$headers = 'From: webmaster@example.com' . "\r\n";

You can also check that mail performed as expected:

if(mail($to,$subject,$message))

And a final thing, you probably don't want to display warnings in a live environment, you can turn those off, either through the ini file or using ini_set.

Kristoffer Sall-Storgaard
  • 10,576
  • 5
  • 36
  • 46
  • true things but not an answer to the question. there are also things that can cause smtp errors that you just cannot no in advance. for example spam blocklists. – The Surrican Feb 06 '11 at 14:52
-2

Best would be to use a library like Swift Mailer that throws exceptions that can be easily handled.

Jan
  • 2,295
  • 1
  • 17
  • 16
  • 4
    this is a cure for the problem but not an answer how to solve it yourself. there are libraries available for almost anything now, and of course they can safe a lot of work. but its a pity to seeing people loosing the ability to solve basic problems themselves because they just dont know any more the basics of the programming language they work in. – The Surrican Feb 06 '11 at 14:53
  • True, it is not an answer to the question but it suggests a way of tackling the problem more pragmatically. – Jan Feb 06 '11 at 14:59