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.
Related
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
I have written code for cron command as below in lumen (micro framwork of laravel)
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\EmailDump;
use DB;
/**
* dumpEmails Class
*
* This cron is to dump emails with cron use
*
* #author Hetal Gohel <hetal.gohel#brainvire.com>
*
*/
class dumpEmails extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'insert:emails';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This cron is to dump emails with cron use';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
echo "1";die;
}
}
in kernel file defined as below,
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\Laravelista\LumenVendorPublish\VendorPublishCommand::class,
'\App\Console\Commands\dumpEmails',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
// protected function schedule(Schedule $schedule)
// {
// return $schedule;
// }
}
I have fired command from console as below,
php artisan insert:emails
while run this command getting error as below,
[Symfony\Component\Console\Exception\CommandNotFoundException] ←[39;49m
←[37;41m There are no commands defined in the "insert" namespace.
please help me to resolve this issue.thank you.
Please remove __construct and keep just the handle method.
Also, when you are listing under $commands at Kernel you need to specify the class.
So your
`\App\Console\Commands\dumpEmails`
becomes
DumpEmails::class
A few extra tips:
Class names are capitalised; (dumpEmails -> DumpEmails)
Add the { to a new line;
This
class dumpEmails extends Command {
should be
class DumpEmails extends Command
{
I also recommend checking out about PSR-x standards. I left one of the blogs I think might help you kick start with them, but go further! :)
Last, but not least, do not forget Command Parent already lets you use its command line potential. So if you wish to output and debug, you can do so by using:
$this->info('Your message to inform');
$this->error('Your error message');
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
This should be straigh forward buti don't know why it is not working . I am creating a command in laravel to send birtday email reminders on a user's birtday .
Everything works fine and the schedule function is triggered but comes with an error
[Symfony\Component\Console\Exception\RuntimeException]
Too many arguments, expected arguments "command".
This is my command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
class SendBirthdayReminderEmail extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'email:birthday';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Email users a birthday Reminder message';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get();
foreach($users as $user) {
Mail::queue('emails.birthday', ['user' => $user], function ($mail) use ($user) {
$mail->to($user['email'])
->from('info#XXXXXX.com', 'Company')
->subject('Happy Birthday!');
});
}
$this->info('Birthday messages sent successfully!');
}
}
And this is my kernel.php file
<?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\SendBirthdayReminderEmail::class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('email:birthday')->dailyAt('13:00')->timezone('Africa/Dar_es_Salaam');
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
Any help will be appreciated . Thanks :-)
I found a solution,
/opt/php70/bin/php /home/sitename/public_html/artisan schedule:run >/dev/null 2>&1
initially i had 1 after schedule:run method . As below
/opt/php70/bin/php /home/sitename/public_html/artisan schedule:run 1 >/dev/null 2>&1
Your code all looks good.
Have you tried simply as below
php artisan schedule:run
after reaching at your root folder path.