How can I run my php script in background to send email notification

541 Views Asked by At

I can send email to all users after submitting the form but it takes some time. What I want to achieve is to skip that task(sending email) and run it in background after submitting the form. So, users can do other things asides waiting to finish the task(sending email).

I tried to look at https://laravel.com/docs/4.2/queues but I'm beginner in Laravel and I don't understand well the documentaion. by the way I'm using old laravel version which is laravel 4.2.

APKFileController.php


$users = User::All();

foreach($users as $user) {
        $data = array(
            'apk_name' => Input::get('name'),
            'version' => $apk->version,
            'download_link' => Input::get('remarks'),
            'subject' => 'v' . $apk->version . ' is now available.',
            'message' => 'A new version of APK has been released!',
        );

        $this->userMailer->sendToApp($user, compact('data'));
    }
}

UserMailer.php

<?php namespace Sample\Mailers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use User;

class UserMailer extends Mailer
{

  public function sendToApp(User $user, $data)
  {
    $subject = $data['data']['subject'];
    $view = 'emails.clients.apkInfo';
    return $this->sendTo($user, $subject, $view, $data);
  }

}

Mailer.php


<?php namespace Sample\Mailers;

use Illuminate\Mail\Mailer as Mail;

abstract class Mailer {

    private $mail;

    function __construct(Mail $mail) {

        $this->mail = $mail;        

    }

    public function sendTo($user, $subject, $view, $data = [] )
    {
        $this->mail->queue($view, $data, function ($message) use ($user, $subject) {
            $message->to($user->email)->subject($subject);
        });
    }
}
2

There are 2 best solutions below

2
Leandro Perini On

You can create a artisan command, just as described here:

Building A Command Generating The Class

To create a new command, you may use the command:make Artisan command, which will generate a command stub to help you get started: Generate A New Command Class

php artisan command:make FooCommand

By default, generated commands will be stored in the app/commands directory; however, you may specify custom path or namespace:

php artisan command:make FooCommand --path=app/classes --namespace=Classes

When creating the command, the --command option may be used to assign the terminal command name:

php artisan command:make AssignUsers --command=users:assign

Then you create a crontab schedule to run your command as described here:

Add a Cron JobPermalink

Open a crontab for your user in a text editor (vi in most distributions):

crontab -e

Note:

To change the text editor used, add the environment variable to your ~/.bashrc file, exchanging vim for nano, or whatever other terminal-based editor you prefer.

export EDITOR=vim

Add the Cron job, save, and exit. The crontab will be saved in /var/spool/cron/crontabsas a crontab specific to the user who created it. To later remove a Cron job from it, delete the line from the user’s crontab file.

0
AudioBubble On

Or you can simple put it in a Queuing system. Refer to this https://laravel.com/docs/4.2/queues