Skip to content

Instantly share code, notes, and snippets.

@morales2k
Created October 1, 2024 20:01
Show Gist options
  • Save morales2k/34d84778868e6efb458721aaf369d4f3 to your computer and use it in GitHub Desktop.
Save morales2k/34d84778868e6efb458721aaf369d4f3 to your computer and use it in GitHub Desktop.
Raw email sender Artisan command to help test email pipelines are well setup; Decoupled from any project specific mailable.
namespace App\Console\Commands;
use Illuminate\Support\Facades\Mail;
use Illuminate\Console\Command;
class SendTestEmailCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:test-email {email?} {subject?} {message?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a test email using the configured mailer.';
public function handle()
{
// Get the optional arguments
$email = $this->option('email') ?: '[email protected]'; // default to "[email protected]"
$subject = $this->option('subject') ?: 'Test Email';
$message = $this->option('message') ?: 'This is a test email sent using the Laravel Artisan command.';
// Send a plain text message using Mail::raw()
Mail::raw([
'subject' => $subject,
'message' => $message,
'from' => config('mail.from.address'),
'to' => $email,
], function ($message) use ($subject, $message) {
$message->subject($subject)
->text($message);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment