How to send mail in Laravel 8 from https:
By: Atsu Emmanuel T.
Step 1: Create a Laravel 7 project
Step 2: Open laravel project set .env configuration
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=your_gmail_id@gmail.com
MAIL_PASSWORD=gmail_password
MAIL_ENCRYPTION=ssl
Step 2: Add Route with routes/web.php
Route::get('send-mail','MailSend@mailsend');
Stage 3: Create Mail
In this progression, we will make mail class SendMail for email sending. Here we will compose code for which view will call and the question of the client. So we should run cry direction. you can check inside application/Mail/SendMail.php
php artisan make:mail SendMail
Add code within SendMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
public function __construct($details)
{
$this->details = $details;
}
public function build()
{
return $this->subject('Mail from Real Programmer')
->view('emails.sendmail');
}
}
Step 4: Create a controller
php artisan make:controller MailSend
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \App\Mail\SendMail;
class MailSend extends Controller
{
public function mailsend()
{
$details = [
'title' => 'Title: Mail from Real Programmer',
'body' => 'Body: This is for testing email using smtp'
];
\Mail::to('siddharthshukla089@gmail.com')->send(new SendMail($details));
return view('emails.thanks');
}
}
Step 5: Create Blade View
In this step, we will make a sharp edge view record and compose an email that we need to send. presently we simply keep in touch with some spurious content. make cry records on “messages” envelope.
resources/views/emails/sendmail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Real Programmer</title>
</head>
<body>
<h1>{{ $details['title'] }}</h1>
<p>{{ $details['body'] }}</p>
<p>Thank you</p>
</body>
</html>
Step 6: Thanks message to show the user. create blade file within
view/emails/thanks.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Thanks</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="alert alert-success" role="alert">
Thanks, we will contact you soon
</div>
</div>
</body>
</html>
Step 7: Change config/mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Real Programmer'),
],
Step 8: Now run the server
php artisan serve
Step 9: now hit on URL
http://localhost:8000/send-mail