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);
});
}
}