Contact US


Get Your Project Done by our experts
info@fundaofwebit.com

How to send email via gmail smtp in laravel 5.8

Whatsapp

https://www.fundaofwebit.com/post/how-to-send-email-via-gmail-smtp-in-laravel-5-8

How to send email via gmail smtp in laravel 5.8


Here, in this article, we are going to learn about how to send an email in laravel 5.8 using gmail smtp.

Step 1: Setup the Laravel 5.8 version as follows:

$ composer create-project --prefer-dist laravel/laravel blog "5.8.*"


Step 2: Now, Go to your gmail.com and Login in it, and then click on Mail icon & Choose  Account. 

Once you are in dashboard, click on Security and scroll to bottom and TURN ON "Less secure app access".


Step 3: Open the project which we created now and open .env file and give the credentials for it:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email_address
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME=fundaofwebit


Step 4: Now, Let's create a mail class which is known as Generating Mailables in Laravel as follows:

$ php artisan make:mail ContactMailable

After Mail successfully created, you will find the file path: app/Mail/ContactMailable:


Step 5: Now, open the Mailable file at app/Mail/ContactMailable and paste the below code as follows: 

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactMailable extends Mailable
{
    use QueueableSerializesModels;
    public $contact_data;

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

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

        $from_name = "Funda of Web IT";
        $from_email = "youremail@gmail.com";
        $subject = "Funda of Web IT: You have a new query";
        return $this->from($from_email$from_name)
            ->view('emails.contact')
            ->subject($subject)
        ;
    }
}


Step 6: Now, we need to create a email template design, file named with contact.blade.php, in this path of resourse/views/emails/contact.blade.php where this template will be sent to email id, Template design given below:

<html>
<head>
    <style>
        .container {
            padding10px;
        }

        #contact {
            border-collapsecollapse;
            width100%;
        }

        #contact td,
        #contact th {
            border1px solid #ddd;
            padding8px;
            width30%;
        }

        #contact th {
            width10%;
        }

        #contact tr:nth-child(even) {
            background-color#f2f2f2;
        }

        #contact tr:hover {
            background-color#ddd;
        }

        #contact th {
            padding-top12px;
            padding-bottom12px;
            text-alignleft;
            background-color#8062c7;
            colorwhite;
        }
        .heading {
            background-colorlightgray;
            padding10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="heading">
            <h1>Funda of Web IT: You a new enquiry from Contact Form</h1>
        </div>
        <table id="customers">
            <tr>
                <th>Full Name</th>
                <td>{{ $contact_data['fullname'}}</td>
            </tr>
            <tr>
                <th>Phone Number</th>
                <td>{{ $contact_data['phone'}}</td>
            </tr>
            <tr>
                <th>Email</th>
                <td>{{ $contact_data['email'}}</td>
            </tr>
            <tr>
                <th>Subject</th>
                <td>{{ $contact_data['subject'}}</td>
            </tr>
            <tr>
                <th>Message</th>
                <td>{{ $contact_data['message'}}</td>
            </tr>
        </table>
    </div>
</body>

</html>


Step 7: Now, create a file controller named ContactController with the following command:

$ php artisan make:controller ContactController


Step 8: go to your controller folder which we just created, path: app/Http/Controllers/ContactController and paste the below code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Mail\ContactMailable;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{

    public function index()
    {
        return view('contact');
    }

    public function mailsending(Request $request)
    {
        $contact_data = [
            'fullname' => $request->input('fullname'),
            'phone' => $request->input('lastname'),
            'email' => $request->input('email'),
            'subject' => $request->input('subject'),
            'message' => $request->input('message'),
        ];
        Mail::to('yourGmailID@gmail.com')->send(new ContactMailable($contact_data));
        return redirect()->back()->with('status','Thank you for contact us. We will get back to asap.!');
    }

}


Step 9: Create a file named index.blade.php inside contact folder as path: resourse/views/contact/index.blade.php and paste the below Form code: 

@extends('layouts.app')

@section('content')

    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="card mt-3">
                    <div class="card-body">
                        <form action="{{ url('contact-submit') }}" method="POST">
                            {{ csrf_field() }}

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">Full Name</label>
                                        <input type="text" name="fullname" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">Phone Number</label>
                                        <input type="text" name="phone" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="">Email Address</label>
                                        <input type="text" name="email" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="">Subject</label>
                                        <input type="text" name="subject" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="">Message</label>
                                        <textarea name="message" rows="3" class="form-control"></textarea>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection


Step 10: Make a route for calling the contact form and submitting the form as follows:

Route::get('contact','ContactController@index');
Route::get('contact-submit','ContactController@mailsending');


Step 11: You are done.! now serve the laravel artisan command as follows:

$ php artisan serve


Thanks for reading.

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT