How can I get an error message for the mail () function?

I am using the PHP mail() function.

If for some reason mail is not sent, I would like to repeat the error message. How should I do it?

Something like

 $this_mail = mail('example@example.com', 'My Subject', $message); if($this_mail) echo 'sent!'; else echo error_message; 

Thank!

+42
php email
Jul 06 '10 at 13:45
source share
7 answers

You can use error_get_last() when mail() returns false.

 $success = mail('example@example.com', 'My Subject', $message); if (!$success) { $errorMessage = error_get_last()['message']; } 

With print_r(error_get_last()) you will get something like this:

[type] => 2
[message] => mail (): Failed to connect to the mail server through port "xxxx" 25, check the settings for "SMTP" and "smtp_port" in php.ini or use ini_set ()
[file] => C: \ www \ X \ X.php
[line] => 2

+66
Nov 25 '13 at 21:37
source share

sending mail in php is not a one-step process. mail () returns true / false, but even if it returns true, this does not mean that the message will be sent. all mail () this adds the message to the queue (using sendmail or something that you installed in php.ini)

there is no reliable way to check if a message has been sent in php. You will have to view the mail server logs.

+9
Jul 6 2018-10-06T00:
source share

You can use the PEAR mailbox , which has the same interface, but returns PEAR_Error when there is a problem.

+2
Jul 06 2018-10-06T00:
source share

There is no error message associated with the mail() function. Only true or false remains if the letter was accepted for delivery. Whether it will ultimately be delivered, but basically whether the domain exists and the address is a correctly formatted email address.

0
Jul 06 '10 at 13:53 on
source share
 $e=error_get_last(); if($e['message']!==''){ // An error function } 

error_get_last (); - returns the last error occurred

0
Apr 20 '17 at 5:44
source share

Try it. If I encounter any error in any file, I received an error message on my email id. Create two index.php and checkErrorEmail.php and upload them to your server. Then download index.php to your browser.

Index.php

 <?php include('checkErrorEmail.php'); include('dereporting.php'); $temp; echo 'hi '.$temp; ?> 

checkErrorEmail.php

 <?php // Destinations define("ADMIN_EMAIL", "pradeep.callus7@hotmail.com"); //define("LOG_FILE", "/my/home/errors.log"); // Destination types define("DEST_EMAIL", "1"); //define("DEST_LOGFILE", "3"); /* Examples */ // Send an e-mail to the administrator //error_log("Fix me!", DEST_EMAIL, ADMIN_EMAIL); // Write the error to our log file //error_log("Error", DEST_LOGFILE, LOG_FILE); /** * my_error_handler($errno, $errstr, $errfile, $errline) * * Author(s): thanosb, ddonahue * Date: May 11, 2008 * * custom error handler * * Parameters: * $errno: Error level * $errstr: Error message * $errfile: File in which the error was raised * $errline: Line at which the error occurred */ function my_error_handler($errno, $errstr, $errfile, $errline) { echo "<br><br><br><br>errno ".$errno.",<br>errstr ".$errstr.",<br>errfile ".$errfile.",<br>errline ".$errline; if($errno) { error_log("Error: $errstr \n error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL); } /*switch ($errno) { case E_USER_ERROR: // Send an e-mail to the administrator error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL); // Write the error to our log file //error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE); break; case E_USER_WARNING: // Write the error to our log file //error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); break; case E_USER_NOTICE: // Write the error to our log file // error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); break; default: // Write the error to our log file //error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); break; }*/ // Don't execute PHP internal error handler return TRUE; } // Use set_error_handler() to tell PHP to use our method $old_error_handler = set_error_handler("my_error_handler"); ?> 
-one
Jul 17 '13 at 10:10
source share

As others have said, tracking down errors for sending mail does not return the logical result of adding mail to the outgoing queue. If you want to track the failure of a successful success, try using SMTP with a mail library such as Swift Mailer, Zend_Mail or phpmailer.

-2
Jul 06 2018-10-06T00:
source share



All Articles