|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fleetbase\FleetOps\Console\Commands; |
| 4 | + |
| 5 | +use Fleetbase\FleetOps\Mail\CustomerCredentialsMail; |
| 6 | +use Fleetbase\FleetOps\Models\Contact; |
| 7 | +use Fleetbase\Models\Company; |
| 8 | +use Fleetbase\Models\User; |
| 9 | +use Illuminate\Console\Command; |
| 10 | +use Illuminate\Support\Facades\Mail; |
| 11 | + |
| 12 | +class TestEmail extends Command |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'fleetops:test-email {email} {--type=customer_credentials : The type of email to test}'; |
| 20 | + |
| 21 | + /** |
| 22 | + * The console command description. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Test FleetOps email templates'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Execute the console command. |
| 30 | + * |
| 31 | + * @return int |
| 32 | + */ |
| 33 | + public function handle() |
| 34 | + { |
| 35 | + $email = $this->argument('email'); |
| 36 | + $type = $this->option('type'); |
| 37 | + |
| 38 | + $this->info('Sending test email...'); |
| 39 | + $this->info("Type: {$type}"); |
| 40 | + $this->info("To: {$email}"); |
| 41 | + |
| 42 | + try { |
| 43 | + switch ($type) { |
| 44 | + case 'customer_credentials': |
| 45 | + $this->sendCustomerCredentialsEmail($email); |
| 46 | + break; |
| 47 | + |
| 48 | + default: |
| 49 | + $this->error("Unknown email type: {$type}"); |
| 50 | + return Command::FAILURE; |
| 51 | + } |
| 52 | + |
| 53 | + $this->info('✓ Test email sent successfully!'); |
| 54 | + return Command::SUCCESS; |
| 55 | + } catch (\Exception $e) { |
| 56 | + $this->error('Failed to send test email: ' . $e->getMessage()); |
| 57 | + return Command::FAILURE; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Send a test customer credentials email. |
| 63 | + * |
| 64 | + * @param string $email |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + private function sendCustomerCredentialsEmail(string $email): void |
| 68 | + { |
| 69 | + // Create a mock user |
| 70 | + $user = new User([ |
| 71 | + 'name' => 'Test Customer', |
| 72 | + 'email' => $email, |
| 73 | + ]); |
| 74 | + |
| 75 | + // Create a mock company |
| 76 | + $company = new Company([ |
| 77 | + 'name' => 'Test Company', |
| 78 | + 'public_id' => 'test_company_123', |
| 79 | + ]); |
| 80 | + |
| 81 | + // Create a mock customer |
| 82 | + $customer = new Contact([ |
| 83 | + 'name' => 'Test Customer', |
| 84 | + 'email' => $email, |
| 85 | + 'phone' => '+1234567890', |
| 86 | + ]); |
| 87 | + |
| 88 | + // Set relations |
| 89 | + $customer->setRelation('company', $company); |
| 90 | + $customer->setRelation('user', $user); |
| 91 | + |
| 92 | + // Mock password |
| 93 | + $plaintextPassword = 'TestPassword123!'; |
| 94 | + |
| 95 | + // Send the email |
| 96 | + Mail::to($email)->send(new CustomerCredentialsMail($plaintextPassword, $customer)); |
| 97 | + } |
| 98 | +} |
0 commit comments