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);
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'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.
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,
];