api.so-manager-dev.com/app/Console/Commands/TestMailCommand.php
Your Name 0b4acd7475
All checks were successful
Deploy api / deploy (push) Successful in 22s
Batch & API
2026-01-16 19:28:13 +09:00

164 lines
5.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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= : 予備メールアドレス}
{--laravel : Laravel Mail を使用(デフォルト: mb_send_mail}';
/**
* コンソールコマンドの説明
*
* @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');
$useLaravel = $this->option('laravel');
// 確認画面
$this->info('=== SHJ-7 メール送信テスト ===');
$this->line('');
$this->line("送信先メールアドレス: {$email}");
$this->line("予備メールアドレス : " . ($backupEmail ?: '(なし)'));
$this->line("テンプレートID : {$templateId}");
$this->line("送信方式 : " . ($useLaravel ? 'Laravel Mail' : 'mb_send_mail'));
$this->line('');
// 環境チェック
$mailDriver = config('mail.default');
$this->warn("現在のメール設定: MAIL_MAILER={$mailDriver}");
if ($useLaravel) {
$this->info("✓ Laravel Mail を使用します(.envの設定に従います");
if ($mailDriver === 'log') {
$this->warn('⚠ メールはログファイルにのみ記録され、実際には送信されません');
$this->warn('⚠ 実際に送信するには .env で MAIL_MAILER=smtp 等に変更してください');
} else {
$this->info("✓ メールは実際に送信されます({$mailDriver}");
}
} else {
$this->warn('⚠ mb_send_mail() を使用しますphp.iniのSMTP設定が必要');
$this->comment(' Laravel Mail を使用する場合は --laravel オプションを追加してください');
}
$this->line('');
// 実行確認
if (!$this->confirm('メール送信を実行しますか?', true)) {
$this->info('キャンセルしました。');
return self::SUCCESS;
}
// メール送信実行
$this->line('');
$this->info('メール送信を開始します...');
try {
$startTime = now();
// SHJ-7サービス呼び出しLaravel Mail または mb_send_mail
if ($useLaravel) {
$result = $this->mailSendService->executeMailSendWithLaravel(
$email,
$backupEmail,
$templateId
);
} else {
$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;
}
}
}