Getting error: `Maximum execution time` when send Mail Queue in Laravel

I using Laravel Mail Queue to send quick mail.

I have an error like:

Maximum execution time of 60 seconds exceeded in SendWelcomeEmail.php
(line 38)

Difficult to describe my error because I don’t have experience with Laravel. So, I tried to record step by step what I did.

My problem is: when user click Send information, Send Mail is activated and it using too much time to complete this work. This affect to the user experience.

I expect an answer or another method to resolve my problem.

My demo was make with step by step:

Step 1:

c:/xampp/htdocs/laravel-test>php artisan queue:table
Migration created successfully!
c:/xampp/htdocs/laravel-test>php artisan queue:failed-table
Migration created successfully!
c:/xampp/htdocs/laravel-test>php artisan migrate
Migrated: 2017_04_03_144759_create_jobs_table
Migrated: 2017_04_03_150557_create_failed_jobs_table

Step 2: update my .env file and setting email:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:beQGwYyUPOTMtkbzDhft7swh68UJW7RqwAGwhELUfLI=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dongxanh
DB_USERNAME=admin
DB_PASSWORD=euEW12il

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database

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=yxpszfarmxoperew
MAIL_ENCRYPTION=tls

Step 3:

php artisan make:mail EmailCustomer

At function __contruct():

protected $content;

public function __construct($content)
{
    $this->content = $content;
}

Function build():

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

Step 4: In views/emails/contact.blade.php is:

Name: {{ $content['name'] }} <br>
Title: {{ $content['title'] }} <br>
Email: {{ $content['email'] }} <br>
Phone number: {{ $content['phonenumber'] }} <br>
Body:

{{ $content['body'] }}

Step 5: Create Job SendWelcomeEmail:

php artisan make:job SendWelcomeEmail

It will create SendWelcomeEmail.php look like this:

use Mail;
use App/Mail/EmailCustomer;

class SendWelcomeEmail implements ShouldQueue
{
    protected $content;

    public function __construct($content)
    {
        $this->content = $content;
    }
    public function handle()
    {
        sleep(60);

        $receiverAddress = '[email protected]';
        $email = new EmailCustomer($content);
        Mail::to($receiverAddress)->queue($email);
    }
}

Finally: Send Job to Queue when user click submits form in app/Http/Controllers/RegisterForm.php:

public function storeCustomer(Request $request) {
    Customer::create($request->all());

    $content = [
        'name'=> $request->input('name'), 
        'title'=> $request->input('title'), 
        'email'=> $request->input('email'),
        'phonenumber'  => $request->input('phonenumber'),
        'body' => $request->input('body')
        ];

    dispatch(new SendWelcomeEmail($content));
    return view('partials.success-mail');
}

I run two commands:

php artisan serve

php artisan queue:work

And tested. It’s show error like the question.

You should not use sleep here , Remove this to make queue work .

If need it try like this to increase time limit

 php artisan queue:work --timeout=0

Or you can use Task Schedule : https://laravel.com/docs/5.4/scheduling

Also Use $this->content not $content.

  $email = new EmailCustomer($content);