Catch an error caused by mail in php

Wikitechy | 1333 Views | php | 03 Jul 2016

 

Scenario :

  • Trying to catch mail error in php? (Error display that occurs while sending email and the mail server is down) 
  • Error that is caused by email server down as below:
<!--2010-02-24T14:26:43+11:00 NOTICE (5): Unexpected Error: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at "ip " port portip, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() (# 2).
Username: admin
Error in line 439 of file D:\test.php
Script: /customer.php
[Global Error Handler]
-->

Fix1 :

  • Define the check condition for mail () function
 if (!mail(...)) {
   // Reschedule email for sending later or inform the error appropriately!
}
  • When the mail() function returns TRUE, it means the mail was successfully accepted for delivery, otherwise it return FALSE.

Fix2 :

  • Simply the PHP Mailer handles this errors as it uses SMTP (Simple Mail Transfer Protocol) as follows:
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

Fix3 :

  • Use the below statement in PHP script.
print_r(error_get_last());
  • The error_get_last() function is used for listing the last generated error.

Applies to :

PHP  5.6.4 PHP 5.5.21 PHP 5.6.5

PHP 5.5.22         PHP 5.6.6 PHP 5.5.23

PHP 5.6.7 PHP 5.5.24 PHP 5.6.8

PHP 5.5.25         PHP 5.6.9  PHP 5.5.26

PHP 5.6.10         PHP 5.5.27 PHP 5.5.31

PHP 5.6.17         PHP 5.5.32         PHP 5.6.18

PHP 5.5.33         PHP 5.6.19 PHP 5.5.34

PHP 5.6.20         PHP 5.5.35 PHP 5.6.21

PHP 7.0.0 PHP 7.0.1 PHP 7.0.2

PHP 7.0.3 PHP 7.0.4 PHP 7.0.5

PHP 7.0.6 PHP 5.6.11 PHP 5.6.12

PHP 5.5.28        PHP 5.5.29 PHP 5.6.13

PHP 5.6.14        PHP 5.6.15 PHP 5.5.30

Related Tags :

  • Php - How can I catch an error caused by mail ()?
  • Catching PHP mail () errors and showing reasonable user error message
  • Php - How can I get the error message for the mail () function?
  • Sets a user-defined error handler function - PHP
  • Php - How can I catch an error caused by mail ()?
  • Php mail returns false no error
  • Php mail not sending no error