Laravel

How to send mail in Laravel 6?

Pinterest LinkedIn Tumblr

Do you want to send mail using SMTP in laravel 6? then I will give your real-time example how to send mail in laravel 6?

In this tutorial, I will guide you on how to send mail in laravel 6. I will create blade file and dynamic information to for mail layout. let’s start from begging.

Step 1: Create a laravel 6 project

laravel new laravel_sendmail

Step 2: Open laravel project set .env configuration

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
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;
   
    /**
     * 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 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('[email protected]')->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', '[email protected]'),
        '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

Write A Comment