Send Emails In Laravel 10 Using Gmail’s SMTP Server

Sending email in Laravel 10; In this example tutorial, you will learn how to send email in Laravel 10 via mailable class using SMTP drivers like Gmail, Mailgun, Postmark, Amazon SES, etc.

Laravel 10 provides a mailable class for sending an email or mail. So, Also, you will learn how to use mailable class to send an email from localhost in Laravel 10 applications.

Send Emails In Laravel 10 Using Gmail’s SMTP Server

Using the following steps, you can send email or mail from localhost using Laravel 10 applications:

  • Step 1 – Create New Laravel 10 Project
  • Step 2 – Setup SMTP with Laravel Project
  • Step 3 – Create a Mailable Class
  • Step 4 – Add Routes
  • Step 5 – Create a Directory And Blade View
  • Step 6 – Create an Email Controller
  • Step 7 – Run Development Server

Step 1 – Create New Laravel 10 Project

Firstly, open your terminal or command prompt.

And execute the following command into it to install or download Laravel 10 application:

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

Step 2 – Setup SMTP with Laravel Project

In this step, Find the .env file from the root directory of Laravel apps. And then Configure smtp details in a .env file like the 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 a Mailable Class

In this step, Create a mailable class, So open the terminal and execute the following command into it to create NotifyMail mailable class:

php artisan make:mail NotifyMail

Once you have created notifyMail class, Then visit the app/mail directory and open notifyMail.php file and add the following code into it:

<?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 the build class of the above-created notifyMail class.

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

In the next step, you need to create an email template named demoMail.blade.php inside the resources/views/emails directory. That’s why, have added the view name email.

Step 4 – Add Routes

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

use App\Http\Controllers\SendEmailController;

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

Step 5 – Create a Directory And Blade View

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

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

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

</body>
</html> 

Step 6 – Create Send Email Controller

In this step, use the following command to create the 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()) {
           echo "Sorry! Please try again latter";
      }else{
           echo "Great! Successfully send in your mail";
         }
    }
}

Step 7 – Run Development Server

In this step, execute the PHP artisan serve command to start your server:

php artisan serve

Then open a browser and hit the following URL on it:

http://127.0.0.1:8000/send-email

Conclusion

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

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 *