Hi devs,
In this tutorial, we will learn Laravel 8 Send Email Example Tutorial
Laravel 8 provides several ways to send your email. You can also use core PHP method for sending an email and you can also use some email service providers such as given sendmail, smtp, mandrill, mailgun, mail, gmail etc. So you can choese any one and set configration.
Follow this step-by-step guide below.
Step 1: .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=dharmiktank128@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
Step 2:Create Route
app/Http/routes.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('/send-mail', [SendMailController::class, 'index'])->name('send.mail.index');
Step 3: Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Mail;
class SendMailController extends Controller
{
public function index()
{
$user = User::find(1)->toArray();
Mail::send('mail', $user, function($message) use ($user) {
$message->to($user['email']);
$message->subject('Welcome Mail');
});
dd('Mail Send Successfully');
}
}
Step 3: Cretae View
resources/views/emails/mailExample.blade.php
Hi {{ $name }}, How are you ?.
run bellow command for quick run:
php artisan serve
open bellow URL on your browser:
http://localhost:8000/send-mail
May this example help you.