0
0
Laravelframework~10 mins

Mail templates in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mail templates
Create Mailable Class
Define build() method
Select Mail Template View
Pass Data to View
Send Mail Using Mailable
Mail Rendered with Template and Data
This flow shows how Laravel uses a Mailable class to select a mail template, pass data, and send the email.
Execution Sample
Laravel
class OrderShipped extends Mailable {
  public $order;

  public function __construct($order) {
    $this->order = $order;
  }

  public function build() {
    return $this->view('emails.shipped')
                ->with(['order' => $this->order]);
  }
}

Mail::to($user)->send(new OrderShipped($order));
This code creates a mail template class that uses a Blade view and sends it to a user with order data.
Execution Table
StepActionEvaluationResult
1Create OrderShipped Mailable instancenew OrderShipped($order)Instance with order data stored
2Call build() methodreturn view('emails.shipped') with order dataSelects Blade template and attaches data
3Call Mail::to($user)->send()Send email using MailableEmail queued for sending with rendered template
4Render Blade templateReplace {{order}} placeholdersFinal email content with order details
5Email sent to userUser receives emailUser sees formatted email with order info
💡 Email sent successfully with the selected template and data.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
orderundefinedOrder object passedPassed to view dataUsed in email contentRemains unchanged
mailableundefinedInstance createdbuild() returns view with dataUsed to send emailInstance used for rendering
email_contentundefinedundefinedRendered with order dataSent to userDelivered email content
Key Moments - 3 Insights
Why do we use a Mailable class instead of sending raw HTML directly?
The Mailable class organizes email logic and templates clearly. As shown in step 1 and 2 of the execution_table, it helps pass data and select templates cleanly.
How does the Blade template get the order data?
In step 2, the build() method attaches the order data with ->with(['order' => $this->order]). This makes the data available inside the Blade view.
When is the email content actually rendered?
Step 4 shows the Blade template rendering happens just before sending, replacing placeholders with real data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at Step 2?
AThe Mailable instance is created
BThe email is sent to the user
CThe build() method selects the mail template and attaches data
DThe Blade template is rendered
💡 Hint
Check the 'Action' and 'Result' columns at Step 2 in the execution_table
At which step is the email content rendered with order details?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look for 'Render Blade template' in the 'Action' column of execution_table
If we do not pass order data in build(), what changes in variable_tracker?
Aemail_content renders without order data
Border remains undefined in all steps
Corder is passed but empty
Dmailable instance is not created
💡 Hint
Check the 'order' row in variable_tracker and consider what happens if data is not passed
Concept Snapshot
Laravel Mail Templates:
- Create a Mailable class with build() method
- Use ->view('template') to select Blade template
- Pass data with ->with([...])
- Send mail with Mail::to()->send(new Mailable)
- Blade renders template with data before sending
Full Transcript
In Laravel, mail templates are managed by creating a Mailable class. This class has a build() method where you select a Blade template and pass data to it. When you send the mail using Mail::to()->send(), Laravel renders the Blade template with the data and sends the email. The process starts by creating the Mailable instance with data, then selecting the template, rendering it with data, and finally sending the email to the user. This organized flow helps keep email logic clean and reusable.