I've been using the PHP mail()
function.
If the mail doesn't send for any reason, I'd like to echo the error message. How would I do that?
Something like
$this_mail = mail('[email protected]', 'My Subject', $message); if($this_mail) echo 'sent!'; else echo error_message;
Thanks!
to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>
Syntax. mail(to,subject,message,headers,parameters);
If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient). Upload the modified php file and retry. Contact your provider if it still does not work. Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.
If you are on Windows using SMTP, you can use error_get_last()
when mail()
returns false. Keep in mind this does not work with PHP's native mail() function.
$success = mail('[email protected]', 'My Subject', $message); if (!$success) { $errorMessage = error_get_last()['message']; }
With print_r(error_get_last())
, you get something like this:
[type] => 2
[message] => mail(): Failed to connect to mailserver at "x.x.x.x" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
[file] => C:\www\X\X.php
[line] => 2
sending mail in php is not a one-step process. mail() returns true/false, but even if it returns true, it doesn't mean the message is going to be sent. all mail() does is add the message to the queue(using sendmail or whatever you set in php.ini)
there is no reliable way to check if the message has been sent in php. you will have to look through the mail server logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With