0

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);
}
Salman Khan Sohoo
  • 97
  • 1
  • 1
  • 13
  • What have you tried to debug the problem? Additionally, have you thought about using a proper service for this? This does not look like something you should have in a controller – Nico Haase Sep 23 '20 at 06:42

1 Answers1

0

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

PatricNox
  • 3,306
  • 1
  • 17
  • 25