I've created a command named SendAutoEmail and i'm running the command by this
php artisan send:autoemail
command is working properly but it's not sending an email, i've added direct Email function into command and also tried to add email function into Controller method and called that controller into Command file but email is not sending but if i'm trying to call the same method via url its sending email successfully, i don't know what is the issue, here is the code
Commands>SendAutoEmail.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\PassportController;
class SendAutoEmail extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'send:autoemail';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$controller = new PassportController();//your controller name
$controller->sendEMail();
dd($controller);
\Log::info('Cron is working fine!');
$this->info('Send:AutoEmail cron Command RUn Seccessfully');
return 0;
}
}
Console>Kernel.php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendAutoEmail;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
SendAutoEmail::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('send:autoemail')
}
}
and the controller method from where i'm trying to send email.
public function sendEMail(){
$alerts = [
'passport_no' => 'ex12312312',
'name' => 'test name',
'expiry_date' => '01-11-2020',
'id_no_cnic' => '12312-3123123-1'
];
$emailSent = Mail::to('test.email#gmail.com')->send(new ExpireAlert($alerts));
DB::table('tbl_test')->insert(
['data_text' => 'test data new']
);
dd($emailSent);
// here on dd($emailSent) i'm getting 0 in response
DB::table('tbl_test')->insert(
['data_text' => 'test data']
);
print_r($alerts);
}
Have you run the scheduler?
php artisan schedule:run
Then you should see your mail in the queue table within your database.
Now, to execute the queue:
php artisan queue:work
In production, the package Supervisor is recommended. What it does is to make sure the worker always is running.
Source: https://laravel.com/docs/8.x/queues#running-the-queue-worker
Related
I'm trying to send an email using a command in Laravel. I would like to send a file from a specific folder. Previously I did it using a view with a form, but now I want to send the email using a command. The file will always be in the same folder.
This is the command code:
<?php
namespace efsystem\Console\Commands;
use Illuminate\Console\Command;
use Storage;
use Mail;
use Config;
class SendEmailEfsystem extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Sending emails to the users';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
);
Mail::send('administracion.email.email_body', $data, function ($message) use ($data) {
$message->to($data['destino']);
$message->subject($data['asunto']);
$message->from(Config::get('mail.username'));
});
$this->info('The emails are send successfully!');
}
}
Since in the "form" you used $request['a_file'] the variable was an instance of Symfony\Component\HttpFoundation\File\UploadedFile wich is an extention of Symfony\Component\HttpFoundation\File\File.
what you need to do is instantiate a File class with the path you have.
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
'a_file' => new \Symfony\Component\HttpFoundation\File\File($pathToFile, true)
);
You can use N69S a_file answer
This is basic tips to help you run your command.
Your Kernel.php must be like this
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendEmailEfsystem::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')->everyMonth();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
Then if you want to run it. Simply run php artisan emails:send
or You want to run it using code you can use Artisan::call('emails:send);
I'm using Laravel 5.3.26 and cannot set the scheduler to run automatically although i have the cron job ready.
I' ve created a new command ligmena:update, below is the code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class ligmena extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'ligmena:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
//
$today = strtotime("now");
$energa_symvolaia = DB::table('symvolaia')->where('eidos_kinisis', '1')->get();
foreach ($energa_symvolaia as $es) {
$imerominia_lixis = strtotime(str_replace("/", "-", $es->imerominia_lixis));
if ($today > $imerominia_lixis)
DB::table('symvolaia')->where('id', '<', $es->id)->update(['eidos_kinisis' => '4']);
}
}
}
?>
Below is the code of Kernel.php
<?php
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\ligmena::class,
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule) {
// $schedule->command('inspire')
// ->hourly();
$schedule->command('ligmena:update')->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands() {
require base_path('routes/console.php');
}
}
?>
I've setup the cron job like this:
php /home/site.com/public_html/testdemo/artisan schedule:run >> /dev/null 2>&1
and it runs every minute.
If I run the command manually it works fine.
Any suggestions?
I 've solved the issue using raw mysql.
The cron job was running fine and the code was ok but it was not changing the DB.
After i changed to raw mysql it worked fine
I am trying to send emails from an scheduled laravel task, when I call the command from the application the email is sent, but when the command is called from the command line, it is executed but there is no email sent.
my command code is the next :
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
try {
Mail::send('emails.testmail', [ ], function ($m) {
$m->to('someona#gmail.com', 'Francisco Larios')->subject('Your Reminder!');
});
} catch (\Exception $e) {
throw new \Exception("Error Processing Request", 1);
}
}
}
the code in the kernel file is the next:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\Inspire::class,
Commands\SendEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
//{
// $schedule->command('inspire')->everyMinute();
//}
}
the command I am running on the console is :
php artisan emails:send
the problem was the smtp server I was using to send the emails, I just change it for another smtp server in my mail.php connfiguration file an so, It works for both application and command line execution.
As part of my Laravel 5.2 application, I would like to define a custom command for artisan, but my command doesn't appear in artisan list.
1). I created the command skeleton: artisan make:console --command=process:emails
2). I added a bit of test code to the handle() method of the new class:
<?php
namespace App\Console\Commands;
use App\CommunicationsQueue;
use Illuminate\Console\Command;
class ProcessEmailQueueCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'process:email';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send all currently pending emails in the queue';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
CommunicationsQueue::where('status', 'PENDING')->update(['status'=>'TEST']);
$this->info('The mails queue was successfully processed.');
}
}
3). Then, I registerred the command in app/Console/Kernel.php:
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
What am I missing here? I'm sure it's something incredibly simple, but I'm not seeing it.
in app/Console/Kernel.php try with the following code
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
On Kernel.php, try include the full name, which should be:
protected $commands = [
\App\Console\Commands\ProcessEmailQueueCommand::class,
];
I am new in laravel. I was trying to make a custom artisan command for creating tables in my test project.I followed this link but my command was not on the artisan list.Event I tried same example given in that link but its also not worked. I dont know why its happening.
I DID THIS:
1) Run this command php artisan make:console SendEmails
2) Put complete class code in app/Console/Commands/SendEmails.php file
<?php
namespace App\Console\Commands;
use App\User;
use App\DripEmailer;
use Illuminate\Console\Command;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'email:send {user}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send drip e-mails to a user';
/**
* The drip e-mail service.
*
* #var DripEmailer
*/
protected $drip;
/**
* Create a new command instance.
*
* #param DripEmailer $drip
* #return void
*/
public function __construct(DripEmailer $drip)
{
parent::__construct();
$this->drip = $drip;
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->drip->send(User::find($this->argument('user')));
}
}
Please help me let me know what I am doing wrong.
You just forgot to Register your command.
This part: https://laravel.com/docs/5.1/artisan#registering-commands.
Open app/Console/Kernel.php and add you command class in $commands array.
like this :
protected $commands = [
Commands\SendEmails::class
];
That's it.