Marcel Gruber
Marcel Gruber

Reputation: 7515

How to customize email subject via emailOutputTo in Laravel schedule call

I'm running tasks via the Laravel scheduler and emailing the results, which is possible because:

The emailOutputTo is exclusive to the command and exec methods.

Logic:

$schedule->call(function () {
    printf("Performing Purge... \n");
    printf("Purge completed! \n");
})->everyMinute()
  ->emailOutputTo('[email protected]');

However, the subject line is always:

Scheduled Job Output For []

and it's not clear or documented as to how we can customize the email subject line.

Upvotes: 1

Views: 1113

Answers (3)

Pieter Claerhout
Pieter Claerhout

Reputation: 348

I guess it's a simple as:

$schedule->command('backup:database')->daily()->name("Nightly Backup")
    ->emailOutputTo('[email protected]')
    ->description('The subject of the mail');

Upvotes: 0

Jonathan
Jonathan

Reputation: 41

In case anyone is still looking for a solution: Use the "name" function to set the title of the email message, e.g.:

   $schedule->command('backup:database')->daily()->name("Nightly Backup")
      ->emailOutputTo('[email protected]');

This will set the title of the email message to "Nightly Backup". This will also become the name of the running command reported by artisan schedule:run, e.g.:

   > php artisan schedule:run
   Running scheduled command: Nightly Backup

Upvotes: 4

Shobi
Shobi

Reputation: 11481

I was not able to find any documentation though, But when I dug the framework code,

in laravel/framework/src/Illuminate/Console/Scheduling/Event.php

the emailOutput() function calls the following function

 protected function getEmailSubject()
 {
     if ($this->description) {
            return $this->description;
     }

     return "Scheduled Job Output For [{$this->command}]";
 }

and this $description is a public property on The Event Class as well, so if you can assign the description somehow, it will work.

I will update this answer once I have solid answer, meanwhile this could lead you.

Upvotes: 0

Related Questions