Send Email In Laravel 5.5 Using Markdown Mailable

By : PHPErrorCode November 23, 2017 680 Views laravel

Laravel is a best php framework so meany developer use in laravel framework, And most developers are looking to send email in the larval framework. If you want to send email in your laravel web application, you're in the right place

Today in this post, we are explain how to send email to laravel web application. For that we use the Markdown Mailables package which provide us pre-built templates and components of mail notifications.

The main advantage in Markdown Mailables package is that you send email in your laravel web application to both the localhost(wamp xampp and lamp) and live server.

Now follow the steps below to send email to your laravel web application

Setp 1: Gmail Account Setup.

To using Gmail SMTP server, you should need to change account access for less secure apps. Don’t worry! Just follow the below 3 line

  1. Login to your google account.
  2. After login gmail Go to this page – https://www.google.com/settings/security/lesssecureapps
  3. From Access for less secure apps section, select Turn on.

Step 2 Create Laravel application

Use the following command to create a new laravel web application

composer create-project --prefer-dist laravel/laravel project_name

Step 3 Create route

Go to routes/web.php file and add below route

Route::get('sendmail', 'ContactController@sendMail');

Step 4: Create Controller

Now let's create a controller, which will be the task of sending mail. Now go to this app/Http/Controllers/ directory and create a controler file name ContactController.php and add following code.

And this contriller need two included file

  • use App\Mail\ContactUs;
  • use Mail;
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\ContactUs;
use Mail;

class SendMailController extends Controller
{
    public function sendMail()
    {
    	$data = [
    		'Name'=> 'phperrorcode.com mail', 
    		'Email'=> 'phperrorcode@gmail.com.',
            'Subject' => 'Subject name',
    		'Message' => 'Message text'
    		];

    	$receiverEmail = 'your email addtess';

    	Mail::to($receiverEmail)->send(new ContactUs($data));

    	dd('Your Email Submit Successfully!!....');
    }
}

Step 5: Install Markdown Mailables package

php artisan make:mail ContactUs --markdown=emails.ContactUs_Templet

Okay, now after executing the above command, two files will be automatically created

  1.  Email/ContactUs.php
  2.  resources/views/emails/ContactUs_Templet.blade.php

If you want to pass the dynamic data in the Email, make some of the following changes in the ContactUs.php file.

namespace App\Mail;

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

class ContactUs extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct($data)
    {
        $this->data = $data;
    }
    public function build()
    {
        return $this->markdown('emails.ContactUs_Templet')
                        ->with('data',$this->data);
    }
}

Now you want to make some changes in the email, open the resources/views/emails/ContactUs_Templet.blade.php file and change it according to your way.

@component('mail::message')

Name :{{ $content['name'] }}

Email :{{ $content['email'] }}

Subject :{{ $content['subject'] }}

Message :{{ $content['description'] }}

@component('mail::table')
|    No   |   Nmae    |  Expert  |
|:------- |:---------:| --------:|
|    1    | Do Johan  | Laravel  |
|    2    | Menthon   | PHP      |
|    3    | Morish    | JAVA     |
@endcomponent

@component('mail::button', ['url' => ''])
{{ $content['button'] }}
@endcomponent

Thanks...
{{ config('app.name') }}

@endcomponent

And you are also customize your template by below command

php artisan vendor:publish --tag=laravel-mail

Now run above command. after you are go to  path resources/views/vendor/mail, where you can change the Template design

Step 6 Set email credentials

in this last step on set email credentials in laravel, so open .env file and some change following variable

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls

Demo is ready, you are ready, let's go run the code following command

php artisan serve

ow you can open following URL on your browser:

http://localhost:8000/sendmail