How to send emails in Laravel

Key takeaways:

  • Laravel simplifies sending emails with built-in functionality that abstracts away the technical details of email service integration.

  • Configure email settings in the .env file, specifying parameters like MAIL_HOST, MAIL_PORT, and MAIL_USERNAME.

  • Create a Mailable class for each email type; here, we use OrderEmail to define an order receipt email.

  • Use a blade view file (order.blade.php) to structure the email’s content.

  • Set up an EmailController to manage email-sending logic within a dedicated class.

  • To execute, call the sendEmail() method in the controller through a defined route.

  • Common use cases include user verification, password resets, order confirmations, and sending newsletters.

Sending emails to the users is a crucial part of any web application. It is the primary way in most applications to communicate with the users. Whether the purpose is to send transactional emails or ongoing offers, emails are the way to go. Laravel, an open-source PHP web framework for developing web applications, offers a simplified way with its built-in functionality for sending emails. Laravel abstracts away the complexities of email sending, which allows us to focus on the content and logic of emails rather than the technical details of email service integrations. It also integrates seamlessly with popular email services like SMTP, Mailgun, and SendGrid to simplify switching between email providers or services without changing the code.

Configurations

First, set up the email credentials in the .env file. In Laravel, the .env file holds configuration details like database connections and SMTP credentials. This uses key-value pairs to store a configuration. We need to update the following environment variables:

MAIL_MAILER="smtp"
MAIL_HOST="smtp.email-provider.com"
MAIL_PORT=465
MAIL_USERNAME="smtp_user"
MAIL_PASSWORD="qwerty1234"
MAIL_ENCRYPTION="ssl"
MAIL_FROM_ADDRESS="email-sender@email-provider.com"
MAIL_FROM_NAME="Application name"

In the code configurations above, the environment variables specify the following:

  • MAIL_MAILER: The email driver to use.

  • MAIL_HOST: The email host provider.

  • MAIL_PORT: The port to send the emails.

  • MAIL_USERNAME and MAIL_PASSWORD: The username and password for authentication.

  • MAIL_ENCRYPTION: The email encryption to use, such as SSL or TLS.

  • MAIL_FROM_ADDRESS: The email sender address.

  • MAIL_FROM_NAME: The name of the email sender.

Sending the email

Let’s consider an example of sending the order receipt to the user. For this purpose, we need to create the following:

  • OrderEmail.php: A mailable class.

  • order.blade.php: A blade view file.

  • EmailController.php: A Laravel controller to make use of the OrderEmail class.

Mailable class

Mailable in Laravel is a particular class responsible for configuring and sending emails. We create a Mailable class for each type of email we send. Create a Mailable class OrderEmail by following the artisan command:

php artisan make:mail OrderEmail
Creating a mailable class

Order view

The content of the email goes into a blade view. Let’s create a view order.blade.php in the location resources/views/emails.

Email controller

Since every controller should be responsible for a single job, a separate controller for sending emails deserves its existence. The following artisan command creates an EmailController class:

php artisan make:controller EmailController
Creating the EmailController class

Putting it all together

Let’s put it all together and see the implementation of each file we created above.

<?php

namespace App\Http\Controllers;

use App\Mail\OrderEmail;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
    public function sendEmail(): void
    {
        $name = 'Kamal';
        $email = 'kamal.ashraf@educative.io';
        $file = public_path('order-receipt.txt');
        $products = [
            [
                'name' => 'Product A',
                'price' => '50',
                'quantity' => 5,
            ],
            [
                'name' => 'Product B',
                'price' => '30',
                'quantity' => 4,
            ],
            [
                'name' => 'Product C',
                'price' => '20',
                'quantity' => 2,
            ]
        ];
        try{
           Mail::to($email)->send(new OrderEmail($name, $products, $file));
        }
        catch(\Throwable $error){
            echo $error->getMessage();
        }
    }
}
Complete implementation to send email
  • In lines 12–13 of app/Http/Controllers/EmailController.php change the name and email variables to match your name and email, and then run the code by pressing the “Run” button.

  • Please check your spam or junk folder if you do not see the email in your inbox.

Here is a line-by-line explanation of all the files above:

  • app/Http/Controllers/EmailController.php

    • Lines 12–13: Storing the name and email of the receiver.

    • Line 14: Storing the file path to send as an attachment.

    • Lines 15–31: Creating a product array.

    • Lines 32–38: Send the email using OrderEmail by passing the name, products, and file as arguments to the constructor.

  • app/Mail/OrderEmail.php

    • Lines 16–21: The class constructor assigns the argument values to the class attributes.

    • Lines 22–27: Create a new Envelope object and set the subject to "Order receipt."

    • Lines 28–37: This method sets the content of the email. It specifies the view to be used as the content and passes the values of name and products to the view.

    • Lines 38–41: The attachment method returns an array of attachments.

  • resources/views/emails/order.blade.php

    • Create the email view by greeting the user with his name and looping over the products to print them in a table.

  • routes/web.php

    • Defines the route to execute the sendEmail() method of EmailController to send the email to the user.

Use cases

Here are some everyday use cases for sending email with Laravel:

  • User registration and verification: After a user registers on the website, we can use Laravel’s built-in email feature to email the user to verify their account.

  • Password reset: When a user forgets his password, Laravel can send the password reset link when he requests a new one.

  • Notifications: This can be used to notify the user via email about specific events, such as when they receive a new message or a friend request.

  • Order confirmation: Upon creating a new order on a Laravel-based e-commerce website, the user should receive a receipt or information about the delivery of the items they purchased.

  • Newsletter subscription or promotions: Laravel’s built-in email functionality can be used to send application users information about new promotions or newsletters to which they are subscribed.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we send email with Laravel using Gmail?

To send email using Gmail in Laravel, set up the .env file with the following SMTP configurations:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_username@gmail.com
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail_username@gmail.com
MAIL_FROM_NAME="Your Name"

Then, use Laravel’s Mail::to() function with a Mailable class to send emails.


How can we send email in Laravel dynamically?

In Laravel, we can send dynamic emails by passing data to a Mailable class. For example, create a Mailable class and use Mail::to($email)->send(new YourMailableClass($data)) where $data contains dynamic content such as user details or custom messages.


Which is the free SMTP server for Laravel?

Gmail, Outlook, and Yahoo offer free SMTP servers that you can use with Laravel for development and testing. Mailtrap is also popular for testing and offers a free plan ideal for Laravel applications in development environments.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved