Send Mail using Gmail SMTP Server Laravel 8 Example.
Teams applying “Send Mail using Gmail SMTP Server Laravel 8 Example.” in a production project can use read the supporting guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi Friends,
It's simple example of laravel 8 sending mail gmail.
We will look at example of laravel 8 send email using google gmail. This post will give you simple example of gmail smtp send email in laravel 8.
I am going to show you example of laravel 8 send mail using gmail smtp.
You can create blade file design and also with dynamic information for mail layout.
In this tutorial, i will give you step by step instruction to send email using google gmail smtp server in laravel 8.
Let's start to example.
Step 1 : Add Configuration
You add send mail configuration with mail driver as gmail server, mail host, mail port, mail username, mail password so laravel 8 will use sender details on email.
env.php
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
First You have to enable google security setting form your gmail.
Second go to Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set as ON.
Step 2 : Create Mail
In this step we will create mail class MyDemoMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow command.
php artisan make:mail MyDemoMail
app/Mail/MyDemoMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyDemoMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from Nicesnippets.com')
->view('emails.myDemoMail');
}
}
Step 3 : Create View Blade
In this step, we will create blade view file and write email that we want to send.
Now we just write some dummy text. create bellow files on "emails" folder.
resources/views/emails/myDemoMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Nicesnippets.com</title>
</head>
<body>
<h1>{{ $details['title'] }}</h1>
<p>{{ $details['body'] }}</p>
<p>Thank you</p>
</body>
</html>
Step 4 : Add Route
Now at last we will create "MyDemoMail" for sending our test email. so let's create bellow web route for testing send email.
routes/web.php
Route::get('send-mail', function () {
$details = [
'title' => 'Mail from Nicesnippets.com',
'body' => 'This is a test email using SMTP'
];
\Mail::to('[email protected]')->send(new \App\Mail\MyDemoMail($details));
dd("Mail Sent Successfully.");
});
Run Your Project:
php artisan serve
http://localhost:8000/send-mail
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial