Rapport-Laravel/app/Console/Commands/TestEmailTemplate.php

116 lines
4.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Mail\NeblogisticAbm;
use Illuminate\Support\Facades\Mail;
class TestEmailTemplate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'email:test-template';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test email templates with sample data';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->info("Testing NeblogisticAbm email template");
// Sample data for the template (matching the mail class expectations)
// Ask user for the neblogistic record ID
$recordId = $this->ask('Enter the neblogistic record ID to load:');
if (!$recordId) {
$this->error("❌ No ID provided.");
return 1;
}
// Load the record from the neblogistic table
$row = \DB::table('neblogistic')->where('id', $recordId)->first();
if (!$row) {
$this->error("❌ No record found with ID: {$recordId}");
return 1;
}
// Map DB fields to the expected $data array for the mail class
$data = [
'id' => $row->id,
'employee_name' => $row->emp_name,
'status' => $row->status,
'submission_id' => $row->submission_id,
'employee_id' => $row->emp_id,
'emp_id' => $row->emp_id, // for template fallback
'emp_email' => $row->emp_email ?? $row->employee_email ?? null,
'employee_email' => $row->emp_email ?? $row->employee_email ?? null,
'emp_phone' => $row->emp_phone ?? $row->employee_phone ?? null,
'employee_phone' => $row->emp_phone ?? $row->employee_phone ?? null,
'dr_name' => $row->doctor_name,
'doctor_mobile_number' => $row->doctor_phone,
'msl_code' => $row->msl_code,
'building_name' => $row->address_building,
'address_line_1' => $row->address_line_1,
'address_line_2' => $row->address_line_2,
'state' => $row->address_state,
'city' => $row->address_city,
'pin_code' => $row->address_pin_code,
// Add more fields as needed for the template
];
try {
// Create the mail instance
$mail = new NeblogisticAbm($data);
// Render the mail to get HTML
$html = $mail->render();
// Save to a file for easy viewing
$outputPath = storage_path("app/email-test-neblogistic-abm.html");
file_put_contents($outputPath, $html);
$this->info("✅ Email template rendered successfully!");
$this->info("📁 HTML saved to: {$outputPath}");
$this->info("🌐 Open this file in your browser to view the email");
// Show preview in console (basic)
$this->newLine();
$this->info("📧 Email Preview:");
$this->line("From: support.rapport@stratawiz.com (GNP Support Team)");
$this->line("Subject: " . $mail->subject);
$this->line("To: {$data['employee_name']}");
$this->line("Status: {$data['status']}");
$this->line("Submission ID: {$data['submission_id']}");
$this->line("Doctor: {$data['dr_name']}");
$this->line("Approval URL: https://stage.buildrapport.co/neblogisticreport/abmapprove?submission_id={$data['submission_id']}&type=cipla_to_doctor&employee_id={$data['employee_id']}");
// Option to send test email
if ($this->confirm('Would you like to send a test email?', false)) {
$testEmail = $this->ask('Enter test email address:');
if ($testEmail) {
Mail::to($testEmail)->send($mail);
$this->info("✅ Test email sent to: {$testEmail}");
}
}
} catch (\Exception $e) {
$this->error("❌ Error rendering template: " . $e->getMessage());
return 1;
}
return 0;
}
}