Laravel: Use Email and Name in Mail::to
Stefan Bogdanescu
Founder & Senior Architect
Beyond the Address: Personalizing Emails with Names in Laravel Mail
Sending transactional emails is a fundamental part of almost any web application. When you integrate a contact form, the next logical step is reliably sending a personalized confirmation or follow-up email. A common hurdle developers face is not just getting the email sent, but ensuring that the recipient's name is correctly included in the message for a professional and personal touch—something beyond just the to address.
This post will walk you through the correct Laravel methodology to handle this requirement, moving from basic email sending to advanced data personalization using Eloquent models.
Understanding the Basics of Mail::to()
When starting with Laravel's Mail facade, the initial approach is straightforward. As noted in the official documentation, the Mail::to() method is designed to accept a single email address, an Eloquent model instance, or a collection of these entities.
For example, if you have an email address stored in your database:
use App\Models\User;
use Illuminate\Support\Facades\Mail;
// Assuming $user is retrieved from the database
$user = User::find(1);
Mail::to($user)->send(new \App\Mail\WelcomeEmail($user));
This method successfully targets the email address. However, as you discovered, passing a name directly via Mail::to() is not supported because the facade itself deals with addresses, not user profiles. To include the name, we must ensure that the information flows from your database (Eloquent) into the Mailable class being sent.
The Solution: Passing Data Through the Mailable Class
The key to solving this lies within the Mailable class itself. A Mailable object is the perfect place to hold all the necessary data for the email, including the recipient's name. When you create a Mailable, you pass the required context into its constructor.
Let's assume we are sending a welcome email and need both the email and the name:
Step 1: Prepare the Data in the Controller (or Service Layer)
First, retrieve the user data you want to send the email to.
use App\Models\User;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
public function sendContactForm(Request $request)
{
// 1. Validate input...
$validated = $request->validate([
'name' => 'required|string',
'email' => 'required|email',
]);
// 2. Find the user (or create one if necessary)
$user = User::where('email', $validated['email'])->first();
if (!$user) {
return back()->withErrors(['email' => 'User not found.']);
}
// 3. Send the email, passing the model instance directly to Mail::to()
Mail::to($user)->send(new \App\Mail\ContactFormSubmitted($user));
return redirect('/thank-you');
}
}
Step 2: Accessing the Name in the Mailable Class
Now, inside your Mailable class (e.g., ContactFormSubmitted.php), you can access any properties of the $user object passed to it. This data is automatically available within the Mailable instance.
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactFormSubmitted extends Mailable
{
use Queueable, SerializesModels;
public $user; // Property to hold the user data
/**
* Create a new message instance.
*
* @param \App\Models\User $user
*/
public function __construct(\App\Models\User $user)
{
// Store the user object so it can be accessed in the view
$this->user = $user;
}
/**
* Build the message.
*
* @return \Illuminate\Mail\Mailables\Message|\Illuminate\Mail\Mailables\Mailable
*/
public function build()
{
return $this->personalizeEmail();
}
protected function personalizeEmail()
{
// Access the name directly from the passed user object
$name = $this->user->name;
return $this->merge([
'userName' => $name,
'emailAddress' => $this->user->email,
])
->view('emails.contact_form_submitted');
}
}
Step 3: Displaying the Name in the Blade View
Finally, in your Blade view, you can use the merged data to display a friendly greeting. This separation of concerns—where the Mailable handles what to send and the View handles how it looks—is a core principle of good Laravel development.
{{-- resources/views/emails/contact_form_submitted.blade.php --}}
<h1>Thank You for Contacting Us, {{ $userName }}!</h1>
<p>We have received your message from: {{ $emailAddress }}</p>
<p>We will be in touch shortly...</p>
Conclusion
While the Mail::to() facade provides the plumbing for addressing emails, true personalization requires leveraging Laravel's object-oriented structure. By ensuring that your Mailable classes accept Eloquent models as parameters and use those models to populate the message content, you achieve robust, maintainable, and highly personalized email communications. This pattern ensures that your application remains decoupled, making it easier to manage data, update logic, and scale your feature set—a hallmark of building powerful applications on the Laravel framework. Always refer back to the official guides on laravelcompany.com for the most up-to-date practices!
Note: Blog content is currently available in English.