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
9,588 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,840 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,840 points
13,486 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,840 points
1,200 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 17, 2020 in Laravel by Niroj
• 82,840 points
24,014 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,840 points
2,103 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,840 points
23,012 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,840 points
2,995 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,840 points
2,918 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,840 points
44,692 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,840 points
12,098 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,840 points
5,276 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP