Jump to content

PHP mail form - Send info when it fails


MainStWebGuy

Recommended Posts

Hello all,

 

I have a couple of simple php mail forms i use on a couple sites for sending basic contact information in email form when submitted. The code is pretty simple, and i use an IF statement to check to be sure the email has been sent, and if it fails, I am notified by email:


#send the email
if(mail($to, $subject, $message, $headers)) {

... send confirmation message...


} 
else {
#notify webmaster
mail($toWebmaster, $failedSubject, $failedMessage, $failedHeaders);
}

My question is... in the $failedMessage, is there a way i can include any error information on why the form might be failing? If so, how would i do something like that?

 

Thanks in advance,

Jason

Link to comment
Share on other sites

Having used the php mail() function for awhile I have found that you really don't get any feedback from it. Even the response (true/false) isn't to be trusted. Many people here will tell you to seek out another mail program such as phpmailer, but I haven't attempted to install it as it seems rather complicated and not necessary for my uses. It may however be something that you want to explore if your need to ensure delivery is strong enough.

  • Like 1
Link to comment
Share on other sites

Thanks Ginerjm

 

I haven't checked into phpmailer in a while, and now i'm wondering if my host already has it as an option for me... i'll check into it.

 

I guess i was wondering if there was a way i can collect php error(s) and send them along in the email, just like how i can "echo" them to the screen while i'm developing to help catch random mistakes in my code.

 

Thanks again!

Jason

Link to comment
Share on other sites

you can get the last php error information using - error_get_last() (returns an array of information.). you can also get the last php error message in  $php_errormsg, but this also requires that the track_errors setting be set to an on/true value, which you can set in your script..

 

the phpmailer class has an $ErrorInfo property that would give your script information about the last error that occurred.

  • Like 1
Link to comment
Share on other sites

Awesome info, and thanks mac_gyver - can you give me a simple code example (I'm a noob man ;) )

 

would i just do something like ....

...

$failedMessage .= $php_errormsg;
$failedMessage .= $ErrorInfo;

...

something like that?

Edited by MainStWebGuy
Link to comment
Share on other sites

What kind of error messages do you even expect?

 

If you're talking about bugs, you should fix them ahead of time with tests. If this is about mail-related issues on your own server, then trying to send out an error mail doesn't make a lot of sense. If it's about mail-related issue on the target server, then PHP's mail() function doesn't cover those.

Link to comment
Share on other sites

I'm hoping to receive tips or hints as to why it might be failing. 

 

I, of course, try to fix any and all bugs before launch through multiple rounds of testing, but, as i'm sure you know, that doesn't always cover everything. I was hoping to add some code that will, in the event of the form not sending, at least get a clue as to where to start looking.

Edited by MainStWebGuy
Link to comment
Share on other sites

I understand the idea, but I'm afraid it doesn't work in practice. Pretty much the only errors you'll get from mail() are related to the mail system itself. And if such a problem occurs, you won't get your alert mails either.

 

mail() is a very primitive function. You seem to think that it thorougly validates the input and tells you exactly what's wrong, but that's not the case.

 

Use normal error logging like everybody else. Or install a global error handler which checks the entire script and sends you a summary of errors. You should also replace the old mail() function with a proper library like PHPMailer to get more detailed messages (as well as more security, more usability, more efficiency).

Link to comment
Share on other sites

 

mail() is a very primitive function. You seem to think that it thorougly validates the input and tells you exactly what's wrong, but that's not the case.

 

Use normal error logging like everybody else.

 

Dear all powerful and all knowing Jacques, 

 

I don't know much, as i'm a noob (stated earlier), so calm down lol.

 

I appreciate your feedback and will look into alternatives.

 

thank you hail, long live, blah blah 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.