Class 'App\Http\Controllers\Mail' not found

Class 'App\Http\Controllers\Mail' not found

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Class 'App\Http\Controllers\Mail' not found

When you encounter a Class 'App\Http\Controllers\Mail' not found error in Laravel, it usually indicates a misunderstanding of how PHP namespaces and class autoloading work within the framework. The specific issue in your scenario stems from a naming conflict between a custom controller method and a built-in helper function, compounded by an incorrect approach to accessing the mailer service.

In the code snippet provided, the developer attempts to send an email using Mail::to($user)->send(new MailNotify($user));. While this syntax is valid in older versions of Laravel or when using the legacy facade directly, the error message suggests that PHP cannot resolve the class App\Http\Controllers\Mail. This specific error path often occurs if a file named Mail.php exists inside the app/Http/Controllers directory.

The Shadowing Problem

Laravel relies on its autoloader to map namespace declarations to physical files in the project structure. When you define:

namespace App\Http\Controllers;

class EmailController extends Controller
{
    // ...
}

PHP expects a file named EmailController.php in the app/Http/Controllers folder. However, if there is also a file named Mail.php located at that same path (perhaps intended to be a custom mail controller or a legacy migration artifact), the autoloader will try to load it when referencing the Mail class if you were using the fully qualified class name or if the namespace context gets confused.

More commonly, though, this specific error arises because of how facades are resolved. The Mail facade in Laravel is an alias for the underlying Illuminate\Mail\Mailer class. If a file named Mail.php sits inside app/Http/Controllers, and that file defines a class also named App\Http\Controllers\Mail, you have created a naming collision.

When your controller runs, PHP looks for App\Http\Controllers\Mail. It finds the local file because of the namespace definition, but if that file does not actually define a class extending Illuminate\Mail\Mailer or simply doesn't exist as expected, you get a "Class not found" error. Conversely, if you intended to use the global facade but have a local file named Mail.php, you might be inadvertently trying to instantiate a non-existent controller class instead of using the service.

Resolving the Conflict

The most robust solution is to remove any custom files named Mail.php from the app/Http/Controllers directory unless they are explicitly part of your application's intended architecture (which is rare for mail logic). The Mail facade should always refer to the global helper class provided by Laravel.

If you have a file Mail.php in that folder, delete it or rename it. Then, ensure your controller uses the facade correctly:

use Illuminate\Support\Facades\Mail; // Or just use Mail if facades are imported globally

class EmailController extends Controller
{
    public function sendEmail()
    {
        $user = auth()->user();
        
        // Ensure you are using the Facade or the helper, not a local class named Mail
        Mail::to($user)->send(new MailNotify($user));

        // Check failures if using a queue or specific mailer configuration
        if (Mail::failures()) {
            return response()->json(['error' => 'Email failed'], 500);
        }

        return response()->json(['message' => 'Successfully sent in your mail']);
    }
}

Modernizing the Approach: The Mailer Interface

While facades like Mail work perfectly fine, relying on them tightly couples your application to the service location pattern. In modern Laravel applications, it is best practice to inject the Mailer interface into your controller constructor. This approach allows you to easily swap mail channels (e.g., switching from SMTP to Postmark or SendGrid) without changing your controller logic.

Here is how you would refactor the code using dependency injection:

use Illuminate\Mail\Mailer; // Or use Illuminate\Contracts\Mail\Mailer
use Illuminate\Http\Request;

class EmailController extends Controller
{
    protected $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function sendEmail()
    {
        $user = auth()->user();

        // Using the injected mailer instance
        $this->mailer->to($user->email)->send(new MailNotify($user));

        if ($this->mailer->failures()) {
            return response()->json(['error' => 'Failed to send email']);
        }

        return response()->json(['message' => 'Email sent successfully']);
    }
}

Handling Failures Correctly

One subtle detail in your original code is the use of Mail::failures(). This method returns an array of failed message instances. It is important to note that checking for failures immediately after sending a single email is often unnecessary unless you are sending multiple emails in one request. If you are processing a list of users, Mail::failures() will catch those specific messages. For a single send operation, the exception thrown by the underlying transport (like SMTP) is usually sufficient to handle errors if your server is misconfigured.

If you rely on third-party email services like Laravel Mailgun or Postmark, ensure that your .env file has the correct credentials configured under MAILGUN_DOMAIN, MAILGUN_SECRET, etc., or that your local config/mail.php points to the correct driver (smtp, mailgun, ses, etc.).

By clearing up namespace collisions and adopting dependency injection, you eliminate the risk of "Class not found" errors caused by shadowing global helpers with local files. This ensures your application remains maintainable and adheres to Laravel's architectural standards.

Our network