How to Send Email in Laravel: The Right Way

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

Laravel makes implementing various web development tasks easier and more sustainable, including sending emails. Sending emails is a common requirement for web applications, whether it’s for user verification, notifications, or newsletters. In this tutorial, we’ll learn how to send emails in Laravel using its built-in features abiding by best practices.

Prerequisites

  • A Laravel application setup
  • Knowledge of Laravel basics
  • An SMTP server or a mail service like Mailgun, SendGrid, etc.

Configuring Mail in Laravel

Before you start sending emails, you need to configure your email settings. Set up your mail configuration in the .env file with your SMTP details. Here’s an example using Mailgun:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="Example App"

Don’t forget to update the config with your real credentials!

Creating Mailables in Laravel

Laravel provides a neat way to organize all the information about an email in a single class called a Mailable. You can generate a new Mailable using Artisan:

php artisan make:mail WelcomeEmail

This command creates a new class in app\Mail\ directory. You can specify the view and build the email like so:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeEmail extends Mailable
{
 use Queueable, SerializesModels;

 public function __construct()
 {
 // Initialization code
 }

 public function build()
 {
 return $this->view('emails.welcome');
 }
}

In your view file, you can design the email’s HTML structure, as Laravel’s default setup uses Blade templates.

Sending Emails

Once your Mailable is set up, sending an email is as simple as using the Mail facade:

use Mail;
use App\Mail\WelcomeEmail;

Route::get('/send-email', function () {
 Mail::to('[email protected]')->send(new WelcomeEmail());
 return 'Email sent';
});

You can also pass dynamic data to the Mailable:


class WelcomeEmail extends Mailable
{
 use Queueable, SerializesModels;

 public $user;

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

 public function build()
 {
 return $this->view('emails.welcome')->with(['user' => $this->user]);
 }
}
Mail::to($request->user())->send(new WelcomeEmail($request->user()));

Queueing Emails

Laravel allows you to queue emails which is very useful for preventing your application from being delayed by the sending time. To send an email on the queue:

use Illuminate\Support\Facades\Mail;

Mail::to('[email protected]')->queue(new WelcomeEmail());

Ensure your application is set up for queue work. Your WelcomeEmail may also implement the ShouldQueue interface so that it’s automatically queued by the mailer.

Testing Emails in Laravel

It’s important to test your email setup. Laravel provides the feature to ‘fake’ mail sending so you can ensure your emails are being sent:

/**
 * Test example for email sending.
 */
public function testEmailSending()
{
 Mail::fake();

 // Perform application logic
 $response = $this->get('/send-email');

 // Assert that an email was sent
 Mail::assertSent(WelcomeEmail::class);
}

This doesn’t actually send the email but ensures that the email would have been sent under normal circumstances.

Conclusion

Sending emails in Laravel is a straightforward process, but the underlying implementation is rich with features like queuing and testing. By following the steps outlined in this tutorial, you can integrate email functionality into your Laravel applications reliably and efficiently.