0
0
LaravelHow-ToBeginner · 4 min read

How to Send Email in Laravel: Simple Guide with Example

In Laravel, you send email using the Mail facade with a mailable class or a closure. Configure your mail settings in .env, then call Mail::to()->send() with your message content or mailable.
📐

Syntax

Laravel uses the Mail facade to send emails. You specify the recipient with to(), then send the email with send(). You can pass a closure or a mailable class to send().

  • Mail::to('email'): sets the recipient email address.
  • send(): sends the email with the provided content or mailable.
  • Closure or Mailable: defines the email content and view.
php
Mail::to('recipient@example.com')->send(new YourMailableClass());
💻

Example

This example shows how to send a simple email using a closure with the Mail facade. It sends a plain text email to a recipient.

php
<?php
use Illuminate\Support\Facades\Mail;

Route::get('/send-email', function () {
    Mail::raw('Hello! This is a test email from Laravel.', function ($message) {
        $message->to('recipient@example.com')
                ->subject('Test Email');
    });
    return 'Email sent successfully!';
});
Output
Email sent successfully!
⚠️

Common Pitfalls

  • Not configuring mail settings in .env properly (like MAIL_MAILER, MAIL_HOST, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD).
  • Forgetting to import the Mail facade or mailable classes.
  • Using synchronous sending in production without queueing, which can slow down response time.
  • Not setting the from address, which can cause emails to be rejected.

Wrong way:

Mail::send('emails.welcome', [], function ($message) {
    $message->to('recipient@example.com');
});

This may fail if from is not set.

Right way:

Mail::send('emails.welcome', [], function ($message) {
    $message->from('sender@example.com', 'Sender Name');
    $message->to('recipient@example.com');
});
📊

Quick Reference

MethodDescription
Mail::to('email')Set recipient email address
Mail::cc('email')Add CC recipient
Mail::bcc('email')Add BCC recipient
Mail::send()Send email with closure or view
Mail::raw()Send plain text email
Mail::queue()Queue email for later sending
Mail::later()Send email after delay (deprecated, use Mail::laterOn() or queue with delay)

Key Takeaways

Configure your mail settings correctly in the .env file before sending emails.
Use the Mail facade's to() and send() methods to send emails easily.
Always set a from address to avoid email delivery issues.
Consider using mailable classes for cleaner, reusable email templates.
Queue emails in production to improve app performance.