Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

email

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!

like image 546
Rohan Avatar asked Jul 06 '10 13:07

Rohan


People also ask

How do you check php mail () is working?

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"; } ?>

What is the syntax of mail () function?

Syntax. mail(to,subject,message,headers,parameters);

Why is my mail php not working?

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.


2 Answers

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

like image 83
user2317245 Avatar answered Oct 02 '22 17:10

user2317245


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.

like image 24
Sergey Eremin Avatar answered Oct 02 '22 17:10

Sergey Eremin