Laravel: Use Email and Name in Mail::to
When building a contact form in Laravel, the immediate goal is often to take the raw name and email fields from your request and dispatch an email. The standard documentation suggests using the Mail facade with the to method. You can pass a plain string like 'example@gmail.com', a User instance, or a collection of users.
\Mail::to('example@gmail.com')->send(new \App\Mail\Hello);
This works perfectly for routing the message, but it leaves a common question unanswered: How do you include the recipient's name in the address line? The documentation states that to accepts an email or a user instance, which implies a specific object structure is required to carry metadata like a display name. However, digging into the Laravel API documentation for the Mail facade reveals that the Mail class itself does not expose a public to function. Instead, this functionality is delegated to the underlying Symfony\Component\Mailer\Addresser component and its Envelope structure.
To pass a name alongside an email address, you cannot simply wrap the string in an array or a custom object unless that object implements the necessary interfaces. The most robust approach involves creating a specific instance of a user-like object or utilizing a helper class that mimics the User contract, specifically implementing the HasName interface concept found in newer Laravel versions, or using the Address class from the underlying mailer stack.
In practice, if you are working with an older version of Laravel (such as 5.5) where the Address class is not directly exposed as a simple string alternative, the standard pattern relies on passing a User model instance. If your application does not have a dedicated User model for every contact form submission, you can create a lightweight DTO or simply construct an array if the mailer configuration allows it, though strict type hinting in the underlying Symfony stack usually expects an object implementing Illuminate\Contracts\Auth\MustVerifyEmail or similar traits depending on the version.
A cleaner solution that avoids creating a full-blown model instance for a one-off email is to use the Symfony\Component\Mailer\Address class directly if you are aware of it, or wrap the data in a class that mimics the expected signature. Here is how you might structure a helper class to hold the name and email:
class ContactEmail {
public function __construct(string $name, string $email) {
$this->name = $name;
$this->email = $email;
}
public function getEmail(): string {
return $this->email;
}
public function getName(): string {
return $this->name;
}
}
Then, you can pass this instance to the mailer. The underlying Symfony component will inspect the object for a getEmail() method and a getName() property or method. If your Laravel version supports it, using a User instance is preferred because it automatically handles authentication traits and ensures the email format is valid.
However, if you are strictly limited to the Mail::to facade in a legacy setup, passing a raw string ignores the name entirely. The facade effectively calls Symfony\Component\Mailer\Addresser with an array of Envelope addresses. To include the name, you must ensure the object passed implements the necessary traits or that the mailer configuration is set up to read from a property named name.
The key takeaway is that the Mail facade is a thin wrapper around Symfony's Mailer. It does not have its own logic for parsing names; it relies on the objects you pass adhering to specific naming conventions or implementing interfaces like MustVerifyEmail. If you find yourself needing to send emails with custom display names frequently, consider defining a dedicated class that implements Illuminate\Contracts\Mail\Mailable and sets the header manually in the view using the $to property if the underlying envelope allows direct manipulation.
For modern Laravel applications (8.x and later), the approach has shifted significantly towards the Mailable interface and the Symfony component directly, where you can set the cc, bcc, and to fields with full name support using the Address class:
use Symfony\Component\Mailer\Address;
$address = new Address('john@example.com', 'John Doe');
$email = (new \App\Mail\Contact())
->to($address)
->send();
If you are maintaining a legacy 5.5 codebase, sticking to the User model pattern is the most reliable way to ensure the name renders correctly in the "To" field of the email header without needing to patch the underlying Symfony configuration manually. Always verify the version-specific behavior, as the exposure of these low-level classes changed between versions.