Run the following command on command prompt to install or download laravel application:
composer create-project --prefer-dist laravel/laravel blog
To configure smtp details in .env file like following:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add user name here
MAIL_PASSWORD=Add password here
MAIL_ENCRYPTION=tls
Note that:- If we are sending a mail using Gmail we have to allow non-secure apps to access Gmail we can do this by going to Gmail settings here.
Once less secure apps are enabled; now we can use Gmail for sending the emails.
Run the following command on command prompt 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 we will create an email template. That 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, Create email template named demoMail.blade.php inside resources/views/emails directory. That’s why 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:
```php
Route::get('email-test', function(){
$details['email'] = 'your_email@gmail.com';
dispatch(new App\Jobs\SendEmailJob($details));
dd('done');
});
To 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 9 Send Email Example</title>
</head>
<body>
<h1>This is test mail from wesley</h1>
<p>Laravel 9 send email example</p>
</body>
</html>
To configuration on queue driver. So open .env file and define database queue driver on “.env” file like following: ``php QUEUE_CONNECTION=database
Then open the command prompt and run following command for queue database tables:
```php
php artisan queue:table
Next, migrate tables into database:
php artisan migrate
Run the following command on command prompt to create queue job:
php artisan make:job SendEmailJob
Then open SendEmailJob.php file which is placed on “app/Jobs” directory. And update the following mail queue code into it:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\SendEmailTest;
use Mail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new SendEmailTest();
Mail::to($this->details['email'])->send($email);
}
}
Run the following command on command prompt to start server locally:
php artisan serve
Then open browser and fire the following URL on it: