Laravel 8 send Bulk Mail in Background using Queue
In this post we will give you Laravel 8 send Bulk Mail in Background using Queue, hear for Laravel 8 send Bulk Mail in Background using Queue we will give you details about it.
So, it’s simple solution is to write your bulk mail login in a queue. so, when you click on send mail then at a time you got a success message “your all bulk mail send in the background”.
If you never use a queue in laravel application then don’t worry in this article I will show you all the steps by steps so, you also make your other functionality on the queue base and make your laravel application faster and smooth.
Step – 1 : Change .env
If, you want to send a bulk mail-in “queue” then make the following two changes in your laravel’s “.env” file.
QUEUE_DRIVER=database
Step – 2 : Create Route
First, we create one route for sending the bulk mail by clicking the button.
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendBulkMailController;
Route::get('send-bulk-mail', [SendBulkMailController::class, 'sendBulkMail'])->name('send-bulk-mail');
Step – 3 : Create a Queue Table
Now, we create a “jobs” table in our database in our laravel application using the following command in terminal.
# create database migration script php artisan queue:table # run our new migration php artisan migrate
Step – 4 : Create Controller
Now, we create our “SendBulkMailController.php” file in “app/Http/Controllers” folder using the following artisan command.
php artisan make:controller SendBulkMailController
then after open the “SendBulkMailController.php” file and write the following code into it.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SendBulkMailController extends Controller
{
public function sendBulkMail(Request $request)
{
$details = [
'subject' => 'Weekly Notification'
];
// send all mail in the queue.
$job = (new \App\Jobs\SendBulkQueueEmail($details))
->delay(
now()
->addSeconds(2)
);
dispatch($job);
echo "Bulk mail send successfully in the background...";
}
}
Step – 5 : Create Jobs Class
Now, we need to create the “SendBulkQueueEmail.php” file in the “app\Jobs” folder using the running the following command in your terminal.
php artisan make:job SendBulkQueueEmail
and open the “app\Jobs\SendBulkQueueemail.php” file and write the following code into it.
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
use Mail;
class SendBulkQueueEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
public $timeout = 7200; // 2 hours
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$data = User::all();
$input['subject'] = $this->details['subject'];
foreach ($data as $key => $value) {
$input['email'] = $value->email;
$input['name'] = $value->name;
\Mail::send('emails.test', [], function($message) use($input){
$message->to($input['email'], $input['name'])
->subject($input['subject']);
});
}
}
}
Step – 6 : Create Mail Blade
Now, we need to create on simple mail blade file in “resources/views/emails/test.blade.php” and write there your mail content.
PHP - I Can't get the $_POST Values on Ajax Request
In this post we will give you information about PHP - I Can't get the $_POST Values on Ajax Request. Hear we will give you detail about PHP - I Can't get the $_POST Values on Ajax RequestAnd how to use it also give you demo for it if it is necessary.
I want to share this posts with you because when i was working on native PHP and Ajax Post request(Specially AngularJS Post request) at that time i can't get Post value on form submit. That's why i would like to share you this post. I also want to tell you i was working on my Ubuntu 14.04. I did try lot but i can't get value.
At last find stuff to how to solve this issue, i did use "file_get_contents('php://input')" that way i could get POST value in json object and i also decode json value using "json_decode()". So let's try this way :
Example:
$post = file_get_contents('php://input');
$post = json_decode($post);
$sql = "INSERT INTO items (title) VALUES ('".$post->title."')";
$result = $mysqli->query($sql);
Hope this code and post will helped you for implement PHP - I Can't get the $_POST Values on Ajax Request. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
This is my test bulk mail.
Step – 7 : Test Bulk Mail Functionality
Now, in the final step we test our bulk mail functionality working properly or not? so, first, start your laravel application using the following artisan command in your terminal.
php artisan serve
now, open the browser and run the following url in it.
http://localhost:/send-bulk-mail
Now, check-in your database’s “jobs” table. in this table, you see like that some entry.
if you want send manually then run the following artisan command in your terminal.
php artisan queue:listen --timeout=0
Hope this code and post will helped you for implement Laravel 8 send Bulk Mail in Background using Queue. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org