-1

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);
        });
    }
}
Sam N Den
  • 57
  • 6
  • 13
  • after reading and searching i found this tools. https://github.com/symfony/process/blob/729f0f798ebf13f17a79e2431bbcc1fd5c515552/Process.php#L250 can someone help if this tool is what im looking for? if so, help me how can I implement. thanks! this is the reference: https://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php/46677126#46677126 – Sam N Den Sep 11 '19 at 07:09

2 Answers2

0

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.

Leandro Perini
  • 384
  • 5
  • 14
  • thank you for your effort. but i can perform cron job after form submitted? i forgot to include to my question that after submitting the form. all users should notify for new apk. – Sam N Den Sep 11 '19 at 02:49
  • Make sure you describe exactly what you need before posting a question. – Leandro Perini Sep 11 '19 at 22:10
0

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