Laravel 7 Send Email Example – onlinecode

Laravel 7 Send Email Example – onlinecode

In this post we will give you information about Laravel 7 Send Email Example – onlinecode. Hear we will give you detail about Laravel 7 Send Email Example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Welcome to you In this article. Today, we will discuss you how to Send Email in Laravel 7. we are using SMTP to send email in this article. laravel provides a swiftmailer package library so we can use that and easily send the mail.

Overview

Step 1: Install Laravel 7

Step 2: Email Configuration

Step 3: Create a Mailable class

Step 4: Create Blade Files

Step 5: Create Route in web.php file

Step 6: Run Our Laravel Application

Step 1 : Install Laravel 7

We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

PHP
composer create-project --prefer-dist laravel/laravel laravel7_sendmail

Step 2: Email Configuration

After complete installation of laravel. we have to mail configuration. now we will open the .env file and define the mail_driver, mail_host, mail_port, mail_username, mail_password and mail_encryption in the .env file. See below changes in a .env file.

PHP
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password
MAIL_ENCRYPTION=tls

Step 3: Create a Mailable class

We will create mail class using below command. Now, if you want to pass object for view then you can write and pass the object in this file. so here we pass the student_detail in the below file..

PHP
php artisan make:mail MySendMail

MySendMail.php

PHP
<?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateContractsQueueShouldQueue;

class MySendMail extends Mailable
{
    use Queueable, SerializesModels;
    public $student_detail;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($student_detail)
    {
        $this->student_detail = $student_detail;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.mySendMail');
    }
}
?>

 

Step 4: Create Blade Files

First we will create the new directory “resources/views/emails” and that directory in create a “resources/views/emails/mySendMail.blade.php” file.

mySendMail.blade.php

PHP
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 7 Send Email Example - onlinecode</title>
</head>
<body>
    <div>
	 {{ $student_detail['first_name'] }}</p>
	 {{ $student_detail['last_name'] }}</p>
	 {{ $student_detail['address'] }}</p>
	 Thank you</p>
    </div>
</body>
</html>

Step 5: Create Route in web.php file

We have to need put below route in routes/web.php file.

PHP
<?php
/*
|--------------------------------------------------------------------------
| 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('/', function () {
   // return view('welcome');	
});
Route::get('/send/email', 'StudentController@mail');
?>

StudentController.php

PHP
<?php
namespace AppHttpControllers;
use IlluminateSupportFacadesMail;
use AppMailMySendMail;

class StudentController extends Controller
{
    public function mail()
	{
	 $student_detail = [
        'first_name' => 'test',
        'last_name' => 'xyz'
        'address' => 'test xyz'
		];
	   Mail::to('test@gmail.com')->send(new MySendMail($student_detail));
	   
	   return 'Email has been sent';
	}
}
?>

Step 6: Run Our Laravel ApplicationWe can start the server and run this example using the below command.

php artisan serve

Now we will run our example using the below Url in the browser.


Please follow and like us:

Hope this code and post will helped you for implement Laravel 7 Send Email Example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US