Create Approval system in Laravel using mailtrap.

mailtrap_login

In this tutorial, I will teach how to sending mail in laravel and how to mail systems used to approve your post and how it will help in big projects.

You have to just follow my steps and understanding the concepts.


Step 1: Open the mailtrap.io

You have open the mailtrap.io in your browser and if you already create an account then click on the login button otherwise signup button click and create a new account.


Step 2: Email Configuration.

After login or signup in mailtrap.io, you have got information about credentials.

You have to put these credentials into your .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME= //username from your mailtrap credentials.
MAIL_PASSWORD= //password from your mailtrap credentials.
MAIL_ENCRYPTION=null

Step 3: Create a Mail sending class.

So open your terminal OR command prompt and run bellow command:

php artisan make:mail NewPostMail

In this step, we have to find App\Mail\NewPostMail.php and put the below code.

<?php

namespace App\Mail;

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

class NewPostMail extends Mailable
{
    use Queueable, SerializesModels;

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

    public function build()
    {
        return $this->view('emails.post')->with("post", $this->post);
    }
}

Step 4: Create Blade Files

In this step, we have to find resources/views and create a new folder name emails & create a new file post.blade.php and put the below code.

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>
	<body>
		<h2>Welcome</h2>
		<br/>
		Your Post is {{ $post['title'] }}
		<br>
		Post created by {{ $post->userDetail->name }}
		<br>
	</body>
</html>

Post-Create Page:

Step 5: Open your Controller.

In this step, we have to find PostController.php in your app/Http/Controller and put the below code on the store method.

You have to use the store method because of store method stores in database & and directly sending mail.

public function store(Request $request){
        $post =  new Post;
        $post->title = $request->title;
        $post->user_id = Auth::User()->id;
        $post->body = $request->body;
        $post->save();

        Mail::to('your mail id which you are login')->send(new NewPostMail($post));
        
        return redirect('posts');
    }

After Post Create:


After Post create you got an email on mailtrap.io


Now we are ready to run our task with laravel , so run bellow command for the run:

php artisan serve

Now you have to open your browser and put below URL:

http://localhost:8000/posts/create

About Dhaval Shah

My name is Dhaval Shah. I'm a PHP developer, entrepreneur and CEO of DAG inventions PVT ltd. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

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

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!