How To Send Email In Laravel 8 With SMTP Example
Today, In this tutorial, you will learn how to send email in laravel 8.
Laravel 8 provide mail class for send email. So, we would like to learn you send email in laravel 8. As well as how to implement sending emails in laravel 8 using a mailable example this tutorial.
you can use several SMTP drivers details (Mailgun, Postmark, Amazon SES, and Sendmail, etc) in .env file for sending email in laravel 8.
In this tutorial, we will learn you step by step on sending emails in laravel 8. So, you need to follow all steps one by one. And you can send an email in laravel 8.
In this step, use the following command to install or download laravel 8 application:
composer create-project --prefer-dist laravel/laravel blog
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
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('emails.demoMail'); } }
In the next step, we will create an email template named demoMail.blade.php inside resources/views/emails directory. That’s why we have added a view name email.
In this step, open /web.php, so navigate to the routes directory. And then add the following routes for send email:
use App\Http\Controllers\SendEmailController; Route::get('send-email', [SendEmailController::class, 'index']);
In this step, create directory name emails inside resources/views directory. Then create a demoMail.blade.php blade view file inside resources/views/emails directory. And update the following code into it:
<!DOCTYPE html> <html> <head> <title>How To Send Email In Laravel 8 With SMTP Example - phpcodingstuff.com</title> </head> <body> <h1>This is test mail from phpcodingstuff.com</h1> <p>How To Send Email In Laravel 8 With SMTP Example</p> </body> </html>
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; class SendEmailController extends Controller { public function sendEmail() { 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'); } } }
In this step, use this PHP artisan serve command to start your server locally:
php artisan serve
I hope it can help you...
Leave a Reply