How can I get the error message for the mail function

0 votes

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('example@example.com', 'My Subject', $message);

if($this_mail) echo 'sent!';
else echo error_message;
Jul 30, 2020 in Laravel by kartik
• 37,520 points
527 views

1 answer to this question.

0 votes

Hello @kartik,

You can use error_get_last() when mai l()returns false.

$success = mail('example@example.com', '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

Hope it helps!!
Thank you!!

answered Jul 30, 2020 by Niroj
• 82,760 points

Related Questions In Laravel

0 votes
1 answer

How Can I Remove “public/index.php” in the URL Generated Laravel?

Hello @kartik, If it isn't already there, create ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,760 points
5,662 views
0 votes
1 answer

How can I echo the version of the current Laravel version in php using the view?

Hello @kartik, This is the way how to ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,760 points
321 views
0 votes
1 answer

I get this error > Trying to get property of non-object . How to fix it?

Hello @aakash, If you want to save grades ...READ MORE

answered Aug 16, 2020 in Laravel by Niroj
• 82,760 points
8,862 views
0 votes
1 answer

How can I clear the cache in laravel using command line?

Hello @kartik, cache:clear artisan command does calls flush function on current ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,760 points
677 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,760 points
7,833 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,760 points
886 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,760 points
708 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,760 points
8,300 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,760 points
6,762 views
0 votes
1 answer

How can I specify the required Node.js version in package.json?

Hello @kartik, I think you can use the ...READ MORE

answered Jul 20, 2020 in Laravel by Niroj
• 82,760 points
2,979 views