Laravel 8 — How to Send Email

In this post, we’ll discuss about how to send email from Laravel 8 from scratch. We’ll use SMTP to send email from Laravel application. After end of this article, you’ll get an idea how send mail works with Laravel.

In this example, we’ll use and create Laravel’s mail class and also configure SMTP credentials in .env file. Laravel supports plenty of drivers from which you can choose one. Here are list of drivers supported by Laravel:

  1. SMTP (we’ll be using this)
  2. Mailgun
  3. Amazon SES
  4. Sendmail
  5. Postmark

We’ll cover simple blade file for email design and how to use that in mail class to send HTML supported mail from Laravel. So without any delay, let’s start:

1. Prepare Configurations

First step is to set proper configurations or credentials in .env file. Make sure you’ve correct email credentials otherwise you’ll get error while sending email. In this example, we’ll use gmail SMTP details to send email. Use this link to get your SMTP credentials — https://support.google.com/a/answer/176600?hl=en

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=codeportal@gmail.com
MAIL_PASSWORD=codeportal123
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=codeportal@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

As you can see, we’ve defined SMTP credentials. You need to replace MAIL_USERNAME and MAIL_PASSWORD with your own credentials.

2. Create Mail Class

Once you’ve done configurations, next step is to create mail class using following command:

php artisan make:mail TestMail

Executing above command will create TestMail.php inside app/Mail/ directory. Replace code of TestMail.php as below:

app/Mail/TestMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class TestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('My Test Email')
                    ->view('emails.testMail');
    }
}

Once you do that, next we’ll work on creating email template using blade file.

3. Create Email Template

Now, its time to create email template using blade template. Create a directory “emails” under resources/views and create testMail.blade.php file inside it.

Once you do that, paste following content inside your testMail.blade.php file

resources/views/testMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>CodePortal</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>-- Team CodePortal</p>
</body>
</html>

Alright, almost done. Next step is to create route which we will use to send email using mail class which will use above email template.

4. Create route to Send Email

To keep this simple and easy, I won’t be creating controller files to send email. We’ll use route file to send email without controller file when someone opens that route in browser.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('test-mail', function () {
   
    $details = [
        'title' => 'Hello there,',
        'body' => 'Greetings from CodePortal!!'
    ];
   
    \Mail::to('harshdoshi999@gmail.com')->send(new \App\Mail\TestMail($details));
   
    dd("Mail sent!!");
});

So, now whenever you open “/test-mail” route, it will send an email to mentioned email address

5. Final Step — Testing

Just run following command to start your application

php artisan serve

and then visit http://localhost:8000/test-mail

You can also find whole code over here — https://github.com/harshdoshi999/laravel-8-send-mail

One thought on “Laravel 8 — How to Send Email

Leave a Reply

Your email address will not be published.

six − six =