2026-07-15

Laravel Mail::send how to pass data to mail View

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Laravel Mail::send how to pass data to mail View

Passing Data from Controller to Mail Views in Laravel: A Developer's Guide

As a senior developer working with Laravel, one of the most common tasks is sending personalized emails. Often, these emails need dynamic content—data fetched from the database or the controller—to make them relevant to the recipient. The core question is always: How do I bridge the gap between my PHP logic (the Controller) and my presentation layer (the Blade Mail View)?

This guide will walk you through the most effective and clean ways to pass data from your Laravel Controller to a customized Mail View, ensuring your emails are dynamic and professional.

The Challenge: Data Flow in Laravel Mails

When you use the Mail facade, you are essentially telling Laravel to render a specific Blade file (the View) and populate it with data before dispatching the email via an underlying mail driver (like Mailtrap or SMTP). The challenge lies in correctly structuring the data so that the view can access it using Blade syntax ({{ $variable }}).

Your example demonstrates the intent perfectly: you have $data in the controller, and you want to insert its contents into emails.auth.registration. We need to ensure this linkage is robust.

Method 1: Passing Data Directly via the Mail Channel (The Simplest Way)

For simple scenarios where you are sending raw data or a small, predefined set of variables directly in the mail command, you can pass an array alongside the view name.

In your controller method, instead of just passing $data, you structure the call to ensure the data is correctly mapped to the view file.

use Illuminate\Support\Facades\Mail;
use App\Mail\RegistrationMail; // Assuming you use a dedicated Mail class

// Inside your Controller method:
$user = User::find($userId); // Example fetching user data
$dataToSend = [
    'login_code' => $user->pidm,
    'password' => $user->password
];

Mail::send('emails.auth.registration', $dataToSend, function ($message) use ($user) {
    $message->to(Input::get('Email'))->subject('Your Login Details');
});

The Mail View (emails.auth.registration.blade.php):

In your view file, you can now access these variables directly:

<p>You can login into our system by using login code and password :</p>
<p><b>Your Login Code :</b> {{ $login_code }}</p> 
<p><b>Your Password :</b> {{ $password }}</p>
<p>Click here to login : <a href="www.mydomain.com/login">Login</a></p>

This method is fast and requires minimal setup, making it excellent for one-off transactional emails. It demonstrates how powerful the integration between Eloquent models and the Mail facade is within Laravel. For more complex scenarios involving business logic, learning about Mailables (as detailed in the official documentation) will be your next step toward scalable architecture on laravelcompany.com.

Method 2: The Professional Approach – Using Mailable Classes

For any application that grows beyond simple greetings, the recommended best practice is to encapsulate the entire email logic within a dedicated Mailable Class. This approach separates your business logic (what data to send) from your presentation layer (how it looks in the view), leading to much cleaner, more maintainable code.

Step 1: Create the Mailable Class

Create a class that defines what data the email needs and how it should be formatted.

php artisan make:mail RegistrationMail

Inside app/Mail/RegistrationMail.php:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class RegistrationMail extends Mailable
{
    use Queueable, SerializesModels;

    public $loginCode;
    public $password;

    public function __construct($loginCode, $password)
    {
        $this->loginCode = $loginCode;
        $this->password = $password;
    }

    /**
     * Build the message.
     */
    public function build()
    {
        return $this->view('emails.auth.registration');
    }
}

Step 2: Update the Controller to Send the Mailable

Now, your controller becomes responsible only for fetching data and dispatching the fully formed Mailable object.

use App\Mail\RegistrationMail;
// ... other imports

// Inside your Controller method:
$user = User::find($userId); 

// Dispatch the Mailable, passing the necessary data directly to the constructor
Mail::send(new RegistrationMail($user->pidm, $user->password));

Step 3: The Clean Mail View

The view remains completely clean, as all the data is handled within the Mailable class.

emails.auth.registration.blade.php:

<p>You can login into our system by using login code and password :</p>
<p><b>Your Login Code :</b> {{ $loginCode }}</p> 
<p><b>Your Password :</b> {{ $password }}</p>
<p>Click here to login : <a href="www.mydomain.com/login">Login</a></p>

Conclusion

While passing data directly via Mail::send() works for very simple tasks, adopting the Mailable pattern is the professional standard in Laravel development. It enforces separation of concerns, makes your code testable, and ensures that the logic governing email content lives with the email itself. By leveraging Mailable classes, you ensure that your application remains scalable and easy to maintain as your features grow.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.