Fixing Missing Names in Laravel Mail::to() When Using Firstname & Lastname
By default, Laravel’s users table includes a single name column, which typically stores the full name. However, many developers prefer to separate firstname and lastname, especially for personalized emails (e.g., "Hallo {firstname}"). This separation can create an issue when sending emails via Mail::to($user), as Laravel expects a name attribute for recipient details. Here’s how to fix it.
The Problem
If you modify your users table schema like this:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});Laravel’s Mail::to($user) will now only include the email, without a name, because Laravel looks for a name attribute, which no longer exists.
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model {
protected function name(): Attribute
{
return Attribute::make(
get: fn () => $this->firstname . ' ' . $this->lastname,
);
}
}This makes $user->name return "Firstname Lastname", ensuring Mail::to($user) works as expected.
Now you can send emails normally:
Mail::to($user)->send(new WelcomeMail());This solution works only if you have no column name on the database, otherwise the Attribute function will be overwritten. If you have a name column in the users table, you either have to rename it, or you go ahead with solution 2:
Solution 2: Explicitly Pass an Array to Mail::to()
If you prefer not to override name, you can manually specify the recipient details:
Mail::to([
['email' => $user->email, 'name' => $user->firstname . ' ' . $user->lastname]
])->send(new WelcomeMail());This approach avoids modifying the model but requires explicit handling each time.
⚠ Be aware to pass an array of arrays!
Key Takeaways
- Laravel expects a
nameattribute when sending mail; removing or misusing it causes emails to lack recipient names. - Use an accessor to dynamically generate
namefromfirstnameandlastname(orvornameandname). - Alternatively, pass an explicit array when calling
Mail::to(). - If your database uses
nameandvorname, Laravel will default to using onlyname(last name). Either rename the columns or use Solution 2.
By applying these fixes, you ensure emails always include the correct recipient name, even when using separate firstname and lastname (or vorname and name). 🚀
