Git Product home page Git Product logo

queue_email_service's Introduction

Step 1: Configure the Laravel

Install it by the following command.

  • composer create-project laravel/laravel --prefer-dist queue_email_service Now, in the .env file, configure the database and email server.

  • BROADCAST_DRIVER=log

  • CACHE_DRIVER=file

  • QUEUE_CONNECTION=database

  • SESSION_DRIVER=file

  • SESSION_LIFETIME=120

  • REDIS_HOST=127.0.0.1

  • REDIS_PASSWORD=null

  • REDIS_PORT=6379

  • MAIL_DRIVER=smtp

  • MAIL_HOST=smtp.gmail.com

  • MAIL_PORT=587

  • [email protected]

  • MAIL_PASSWORD=password

  • MAIL_ENCRYPTION=tls

Step 2: Create mail configuration for sending mail.

We will create one route to send an email and also create one controller called EmailController.

  • php artisan make:controller EmailController

In routes >> web.php file, add the following code.

  • Route::get('email', 'EmailController@sendEmail');

Step 3: Configure Queue.

Now, we will need to create the job that purpose is to send the actual email to the user.

  • php artisan queue:table');
  • php artisan migrate');
  • php artisan make:job SendEmailJob'); Now, the send email function will reside in the job file. So that job file looks like this.
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

class SendEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to('[email protected]')->send(new SendMailable());
    }
}

Step 4: Configure the EmailController like this

<?php
namespace App\Http\Controllers;
use App\Jobs\SendEmailJob;
use Auth;
use Carbon\Carbon;

class EmailController extends Controller {
	public function sendEmail() {
		$user = Auth::user();
		$emailJob = (new SendEmailJob($user))->delay(Carbon::now()->addSeconds(10));
		dispatch($emailJob);
		echo 'email sent';

	}
}

queue_email_service's People

Contributors

asmaulhasnat avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.