MainStWebGuy Posted June 20, 2016 Share Posted June 20, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/ Share on other sites More sharing options...
ginerjm Posted June 20, 2016 Share Posted June 20, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533838 Share on other sites More sharing options...
MainStWebGuy Posted June 20, 2016 Author Share Posted June 20, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533839 Share on other sites More sharing options...
mac_gyver Posted June 20, 2016 Share Posted June 20, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533840 Share on other sites More sharing options...
MainStWebGuy Posted June 20, 2016 Author Share Posted June 20, 2016 (edited) 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 June 20, 2016 by MainStWebGuy Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533841 Share on other sites More sharing options...
Jacques1 Posted June 20, 2016 Share Posted June 20, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533845 Share on other sites More sharing options...
MainStWebGuy Posted June 21, 2016 Author Share Posted June 21, 2016 (edited) 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 June 21, 2016 by MainStWebGuy Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533858 Share on other sites More sharing options...
Jacques1 Posted June 21, 2016 Share Posted June 21, 2016 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). Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533864 Share on other sites More sharing options...
MainStWebGuy Posted June 21, 2016 Author Share Posted June 21, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533868 Share on other sites More sharing options...
Jacques1 Posted June 21, 2016 Share Posted June 21, 2016 No need to get butthurt over a few simple facts. Quote Link to comment https://forums.phpfreaks.com/topic/301368-php-mail-form-send-info-when-it-fails/#findComment-1533870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.