How to Send Email in Laravel 10 Project?

Mar 06, 2023 . Admin


Hi all,

In this example, you will learn how to send email in laravel 10 project?. We will look at example of example of send email in laravel 10. In this article, we will implement a laravel 10 send mail using smtp example. if you have question about laravel 10 sends mail example and view then I will give simple example with solution. you will do the following things for step-by-step send mail laravel 10 using SMTP.

In this tutorial, we will discuss how to configure our Laravel 10 applications to send emails using your Gmail account as a Gmail SMTP server with the default Laravel SMTP configurations.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Make Configuration

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 10 will use those sender configurations for sending the email. So you can simply add as like following.

.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=xyz123456
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Create Mail Class

In this step, we will create a mail class DemoMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow the command.

php artisan make:mail SendMail

now, let's update code on SendMail.php file as bellow:

app/Mail/SendMail.php
<?php

namespace App\Mail;

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

class SendMail extends Mailable
{
    use Queueable, SerializesModels;

    public $mailData;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from Mywebtuts.com')
                    ->view('emails.sendDemoMail');
    }
}
Step 4: Create Controller

In this step, we will create SendMailController with index() method where we write code for sending mail to given email address. so first let's create controller by following command and update code on it.

php artisan make:controller SendMailController

Now, update the code on the SendMailController file.

app/Http/Controllers/SendMailController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
use App\Mail\SendMail;

class SendMailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Send mail from Mywebtuts.com',
            'body' => 'This is for testing email using smtp.'
        ];
         
        Mail::to('yourmail@gmail.com')->send(new SendMail($mailData));
           
        dd("Email is sent successfully.");
    }
}
Step 5: Create Routes

In this step, we need to create routes for a list of sending emails. so open your "routes/web.php" file and add the following route.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\SendMailController;
  
/*
|--------------------------------------------------------------------------
| 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('mail-send', [SendMailController::class, 'index']);
Step 6: Create Blade View

In this step, we will create a blade view file and write the email that we want to send. now we just write some dummy text. create bellow files on the "emails" folder.

resources/views/emails/sendDemoMail.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Mywebtuts.com</title>
</head>
<body>
    <h1>{{ $mailData['title'] }}</h1>
    <p>{{ $mailData['body'] }}</p>
  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
     
    <p>Thank you</p>
</body>
</html>
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/mail-send
Output:

I hope it can help you...

#Laravel 10