147 lines
4.7 KiB
PHP
147 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Log;
|
||
use App\Services\ShjMailSendService;
|
||
|
||
/**
|
||
* メール送信テストコマンド
|
||
*
|
||
* SHJ-7メール送信機能をテストするための便利なコマンド
|
||
* 開発・検証環境で使用することを想定
|
||
*/
|
||
class TestMailCommand extends Command
|
||
{
|
||
/**
|
||
* コンソールコマンドの名前とシグネチャ
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $signature = 'test:mail
|
||
{email? : 送信先メールアドレス(省略時: wyf_0506@hotmail.com)}
|
||
{--template=205 : テンプレートID(デフォルト: 205=SHJ-4B用)}
|
||
{--backup= : 予備メールアドレス}';
|
||
|
||
/**
|
||
* コンソールコマンドの説明
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $description = 'SHJ-7メール送信機能のテストコマンド - 指定したメールアドレスにテストメールを送信';
|
||
|
||
/**
|
||
* SHJメール送信サービス
|
||
*
|
||
* @var ShjMailSendService
|
||
*/
|
||
protected $mailSendService;
|
||
|
||
/**
|
||
* コンストラクタ
|
||
*
|
||
* @param ShjMailSendService $mailSendService
|
||
*/
|
||
public function __construct(ShjMailSendService $mailSendService)
|
||
{
|
||
parent::__construct();
|
||
$this->mailSendService = $mailSendService;
|
||
}
|
||
|
||
/**
|
||
* コマンド実行
|
||
*
|
||
* @return int
|
||
*/
|
||
public function handle()
|
||
{
|
||
// パラメータ取得
|
||
$email = $this->argument('email') ?? 'wyf_0506@hotmail.com';
|
||
$backupEmail = $this->option('backup') ?? '';
|
||
$templateId = (int) $this->option('template');
|
||
|
||
// 確認画面
|
||
$this->info('=== SHJ-7 メール送信テスト ===');
|
||
$this->line('');
|
||
$this->line("送信先メールアドレス: {$email}");
|
||
$this->line("予備メールアドレス : " . ($backupEmail ?: '(なし)'));
|
||
$this->line("テンプレートID : {$templateId}");
|
||
$this->line('');
|
||
|
||
// 環境チェック
|
||
$mailDriver = config('mail.default');
|
||
$this->warn("現在のメール設定: MAIL_MAILER={$mailDriver}");
|
||
|
||
if ($mailDriver === 'log') {
|
||
$this->warn('⚠ メールはログファイルにのみ記録され、実際には送信されません');
|
||
$this->warn('⚠ 実際に送信するには .env で MAIL_MAILER=smtp 等に変更してください');
|
||
} else {
|
||
$this->info("✓ メールは実際に送信されます({$mailDriver})");
|
||
}
|
||
|
||
$this->line('');
|
||
|
||
// 実行確認
|
||
if (!$this->confirm('メール送信を実行しますか?', true)) {
|
||
$this->info('キャンセルしました。');
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
// メール送信実行
|
||
$this->line('');
|
||
$this->info('メール送信を開始します...');
|
||
|
||
try {
|
||
$startTime = now();
|
||
|
||
// SHJ-7サービス呼び出し
|
||
$result = $this->mailSendService->executeMailSend(
|
||
$email,
|
||
$backupEmail,
|
||
$templateId
|
||
);
|
||
|
||
$endTime = now();
|
||
$duration = $startTime->diffInSeconds($endTime);
|
||
|
||
$this->line('');
|
||
$this->info('=== 実行結果 ===');
|
||
|
||
if ($result['result'] === 0) {
|
||
$this->info('✓ メール送信成功');
|
||
$this->line("処理時間: {$duration}秒");
|
||
|
||
if ($mailDriver === 'log') {
|
||
$this->line('');
|
||
$this->comment('※ ログファイルを確認してください:');
|
||
$this->comment(' storage/logs/laravel.log');
|
||
} else {
|
||
$this->line('');
|
||
$this->comment("※ {$email} のメールボックスを確認してください");
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
} else {
|
||
$this->error('✗ メール送信失敗');
|
||
$this->error("エラー情報: {$result['error_info']}");
|
||
$this->line('');
|
||
$this->comment('ログファイルで詳細を確認してください: storage/logs/laravel.log');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
} catch (\Exception $e) {
|
||
$this->error('✗ 予期しないエラーが発生しました');
|
||
$this->error("エラー: {$e->getMessage()}");
|
||
|
||
Log::error('test:mail コマンドエラー', [
|
||
'exception' => $e->getMessage(),
|
||
'trace' => $e->getTraceAsString()
|
||
]);
|
||
|
||
return self::FAILURE;
|
||
}
|
||
}
|
||
}
|