Send Email Using Mail Function in Laravel 5.7 or Laravel 5.8

Laravel 5.8 Sending Email by Mail Function, Send Mail Using Mail Function in Laravel 5.8, Send Mail Using Mail Function in Laravel 5.7, Laravel 5.8 Send Email with Mail Function

 

Hello friends So today I will tell you through these tutorials how to send mail through laravel 5.7 or laravel 5.8 framework. I will tell you step to step through this tutorial.

Overview

  1. Create Mail Form (Blade File).
  2. Changes .env file.
  3. URL Add into Route File.
  4. Create Controller File.

 

Step 1:- Create Mail Form (Blade File).

 

This is the first step, in this step, we will create a form. In this form we will input an email.And save this form with the blade extension inside this resources/views/mailform.blade.php folder.

 

resources/views/mailform.blade.php

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Send Mail Using Mail Function in Laravel 5.7 or Laravel 5.8 - Experts PHP</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<body>
<div class="about-banner">
</div>
<div class="contact-background"> 
<div class="contactussec">
<div class="container">
<div class="col-md-4">
<div class="contactformsec clearfix">
<form action="{{url('send')}}" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<label>Your Email Address</label>
<input type="email" class="form-control" name="email">
<input type="submit" name="submit" value="Submit" class="submit-btn">
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

 

Step 2:- Changes .env file.

 

In this step we will change the .env file.In place of MAIL_DRIVER = smtp, you have to replace MAIL_DRIVER = mail.

.env file

MAIL_DRIVER=mail

 

Step 3:- URL Add into Route File.

 

In this step, we will add the url of the mailform of the given post method post url to the route file. You can see below for how to add this url to the file of the route.

routes/web.php

Route::post('send','sendController@send');

 

Step 4 :- Create Controller File

 

Create the controller named sendController.php.After creating the controller, the code of the mail will be written. For that you can see in the given controller below.

 

Controllers/sendController.php

 

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Redirect;
use View;
use Mail;

class sendController extends Controller
{
public function send(request $req)
{
$mailid = $req->email;
$subject = 'News Information.';
$data = array('email' => $mailid, 'subject' => $subject);
Mail::send('emails.newsinfo', $data, function ($message) use ($data) {
$message->from('expertsphp@gmail.com', 'News Information');
$message->to($data['email']);
$message->subject($data['subject']); 
});
return redirect()->back()->with('message','Successfully Send Your Mail Id.');
}
}

 

Then after that create newsinfo blade file and write some text info into this file and save into emails folder.

 

resources/views/emails/newsinfo.blade.php

 

<!DOCTYPE html>
<html lang="en">
<head>
<title>News Info</title>
</head>
<body>
<h1>Send Mail Using Mail Function in Laravel 5.7 or Laravel 5.8 - Experts PHP</h1>
</body>
</html>