Send Email in Laravel Example Tutorial Step by Step Guide

Sending email in laravel or any other project is very required. So, here we implement how to send email in laravel app example easy way step by step.

Many times we need to send mail to user which we want invite or direct send email using the form. This example show you Send Email in Laravel Tutorial Step by Step Guide easy way.

Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice. Here we use Mail function to sending email to users.

Step 1: Update SMTP Details in .env File

First you need to update the mailtrap details in your .env file just like below, You can send email via log also. If don’t have mailtrap details visit this link and create a new mailtrap account.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

Step 2: Add Routes

Now create two routes in your web.php file one for showing form and another is sending email to the user which you want. So, open the routes/web.php file and update the below routes on it;

Route::get('/email', 'EmailController@create');
Route::post('/email', 'EmailController@sendEmail')->name('send.email');

Step 3: Update Controller

You can create new controller or update these methods in your existing controller. So, create a new controller using the following command:

php artisan make:controller EmailController

After successfully create new controller, open the app\Http\Controllers\EmailController.php file and update below code on it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class EmailController extends Controller
{
    public function create()
    {

        return view('email');
    }

    public function sendEmail(Request $request)
    {
        $request->validate([
          'email' => 'required|email',
          'subject' => 'required',
          'content' => 'required',
        ]);

        $data = [
          'subject' => $request->subject,
          'email' => $request->email,
          'content' => $request->content
        ];

        Mail::send('email-template', $data, function($message) use ($data) {
          $message->to($data['email'])
          ->subject($data['subject']);
        });

        return back()->with(['message' => 'Email successfully sent!']);
    }
}

Step 4: Create Blade FIle

Now you need to create a blade file where you show email send form in laravel, So, navigate the resources/views directory and crate email.blade.php file. After that open resources\views\email.blade.php file and update the below code.

<!DOCTYPE html>
<html>
<head>
    <title>Mail Send in Laravel Example with</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>

<body>
    <div class="container">
        <h1>Mail Send in Laravel Example with <a href="https://codingdriver.com/">Coding Driver</a></h1>
          @if(session()->has('message'))
              <div class="alert alert-success">
                  {{ session()->get('message') }}
              </div>
          @endif
        <form action="{{ route('send.email') }}" method="post">
          @csrf
            <div class="form-group">
                <label>Email:</label>
                <input type="email" name="email" class="form-control" placeholder="Enter Email">
                @error('email')
                  <span class="text-danger"> {{ $message }} </span>
                @enderror
            </div>

            <div class="form-group">
                <label>Subject:</label>
                <input type="text" name="subject" class="form-control" placeholder="Enter subject">
                @error('subject')
                  <span class="text-danger"> {{ $message }} </span>
                @enderror
            </div>

            <div class="form-group">
                <strong>Content:</strong>
                <textarea name="content" rows="5" class="form-control" placeholder="Enter Your Message"></textarea>
                @error('content')
                  <span class="text-danger"> {{ $message }} </span>
                @enderror
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-success save-data">Send</button>
            </div>
        </form>
    </div>
</body>
</html>

Step 5: Email Template

When we send a email then we need to a template which is show the message. So, you need to create an another file named as email-template.blade.php file. After that open the resources\views\email-template.blade.php file and put the below code on it;

<div class="container">
     <div class="row justify-content-center">
         <div class="col-md-8">
             <div class="card">
                 <div class="card-header">Welcome!</div>
                   <div class="card-body">
                    @if (session('resent'))
                         <div class="alert alert-success" role="alert">
                            {{ __('A fresh mail has been sent to your email address.') }}
                        </div>
                    @endif
                    {!! $content !!}
                </div>
            </div>
        </div>
    </div>
</div>

Great! You successfully lean Email Send Example in Laravel. I hope its works for you. If you have any question don’t hesitate to dropping a message below.

6 thoughts on “Send Email in Laravel Example Tutorial Step by Step Guide”

  1. Pingback: Correo en Laravel – DAM
  2. Hi,
    thanks for your code.

    I have tried it, but it doesn’t work.
    This is my web.php:
    name(‘send.email’);

    and I have created the EmailController with the sentence:
    php artisan make:controller EmailController

    I receive the error when I access to the route /email:
    Illuminate\Contracts\Container\BindingResolutionException
    Target class [EmailController] does not exist.

    Do you you have all the code in github?
    Can you help me?

    Thanks

    Reply
    • Please create EmailController, if you are using another controller then update the controller in web.php file.

      Reply

Leave a Comment