Performance: Mailable classes
This affects server-side email generation and delivery speed, impacting backend response time and perceived user experience.
Jump into concepts and practice - no test required
use Illuminate\Mail\Mailable; class WelcomeMail extends Mailable { public function build() { return $this->subject('Welcome') ->view('emails.welcome'); } } Mail::to('user@example.com')->queue(new WelcomeMail());
Mail::html('<h1>Welcome!</h1>', function ($message) { $message->to('user@example.com') ->subject('Welcome'); });
| Pattern | Server Blocking | Template Reuse | Queue Support | Verdict |
|---|---|---|---|---|
| Inline Mail::send() | Blocks request | No reuse | No | [X] Bad |
| Mailable class with immediate send | Blocks request | Yes | No | [!] OK |
| Mailable class with queue() | Non-blocking | Yes | Yes | [OK] Good |
Mailable class in Laravel?build() method is where you define the email's view and subject in Laravel Mailable classes.send(), compose(), and render() are not used in Laravel Mailable classes for this purpose.class OrderShipped extends Mailable {
public function build() {
return $this->subject('Your order has shipped')->view('emails.shipped');
}
}build() method sets the subject using subject('Your order has shipped').class WelcomeUser extends Mailable {
public $user;
public function __construct($user) {
$this->user = $user;
}
public function build() {
return $this->view('emails.welcome')->with('user');
}
}with() method requires a key-value pair or an array to pass data to the view, but here it only has a key string without a value.build() is correct.build() method?with() method accepts an associative array to pass multiple variables to the view.