Laravel 8 Send Email Example

Laravel 8 Send email example; In this tutorial, you will learn how to send email in laravel 8 using SMTP drivers like Mailgun, Postmark, Amazon SES, and send email.

Laravel 8 provides a mail class for sending an email. So, we would like to show you send an email from localhost using mailable in laravel 8 applications.

Note that, you can use several SMTP drivers details (Mailgun, Postmark, Amazon SES, and sendmail) in .env file for sending email in laravel 8.

Laravel 8 Send Email Example

Follow the below steps and send email or mail from localhost using laravel 8 applications:

  • Step 1 – Install Laravel 8 App
  • Step 2 – Configuration SMTP in .env
  • Step 3 – Create Mailable Class
  • Step 4 – Add Email Send Route
  • Step 5 – Create Directory And Blade View
  • Step 6 – Create Email Controller
  • Step 7 – Run Development Server

Step 1 – Install Laravel 8 App

In this step, use the following command to install or download laravel 8 application:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Configuration SMTP in .env

In this step, you need to configure smtp details in .env file like following:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending the emails.

Step 3 – Create Mailable Class

In this step, use the below given command to create NotifyMail mailable class:

php artisan make:mail NotifyMail
<?php

namespace App\Mail;

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

class NotifyMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('view.name');
    }
}

By whatever name you will create an email template. That you want to send. Do not forget to add an email template name in build class of the above created notifymail class.

    return $this->view('view.name');
    to
    return $this->view('emails.demoMail');

In next step, we will create email template named demoMail.blade.php inside resources/views/emails directory. That’s why we have added view name email.

Step 4 – Add Send Email Route

In this step, open /web.php, so navigate to routes directory. And then add the following routes for send email:

use App\Http\Controllers\SendEmailController;

Route::get('send-email', [SendEmailController::class, 'index']);

Step 5 – Create Directory And Blade View

In this step, create directory name emails inside resources/views directory. Then create an demoMail.blade.php blade view file inside resources/views/emails directory. And update the following code into it:

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 8 Send Email Example</title>
</head>
<body>

 <h1>This is test mail from Tutsmake.com</h1>
 <p>Laravel 8 send email example</p>

</body>
</html> 

Step 6 – Create Send Email Controller

In this step, use the following command to create controller name SendEmailController:

php artisan make:controller SendEmailController

Then navigate to app/Http/Controllers directory and open SendEmailController.php. Then update the following code into it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Mail;

use App\Mail\NotifyMail;


class SendEmailController extends Controller
{

    public function index()
    {

      Mail::to('receiver-email-id')->send(new NotifyMail());

      if (Mail::failures()) {
           return response()->Fail('Sorry! Please try again latter');
      }else{
           return response()->success('Great! Successfully send in your mail');
         }
    }
}

Step 7 – Run Development Server

In this step, use this PHP artisan serve command to start your server locally:

php artisan serve

Then open browser and fire the following URL on it:

http://127.0.0.1:8000/send-email

Conclusion

Laravel 8 send an email example, you have learned how to create a mailable class in laravel 8. And using this class on how to send emails in laravel 8.

Recommended Laravel Posts

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *