# php Exception エラーキャッチでメール送信

exception の使い方 (opens new window)


try {
        throw new \Exception("Hello");
} catch(\Exception $e) {
        echo $e->getMessage()." catch in\n";
        throw $e;
} finally {
        echo $e->getMessage()." finally \n";
        throw new \Exception("Bye");
}

try {
    throw new Error( "foobar" );
    // or:
    // throw new Exception( "foobar" );
}
catch (Throwable $e) {
    var_export( $e );
}

# Exception class 内にある protected メソッド

メソッド 説明
getMessage() 例外メッセージ
getCode() 例外コード
getFile() ファイル名
getLine() 発生行目
getTrace() バックトレース
getTraceAsString() バックトレースの文字列

オーバーライド可能なメソッド

  • __toString() // 例外の文字列表現
  • __construct($message = null, $code = 0) // 例外を作成する

# laravel mail 送信


 // 送信先
 $sendto = [
     'admin@php-exception.com',
 ];

$text = $e->getMessage() . "\n File: " . $e->getFile() . "\n Line: " . $e->getLine() . "\n Trace:" . $e->getTraceAsString();

 // メール送信
 Mail::raw($text, function ($e) use ($subject, $sendto) {
     $sendfrom = 'system@php-exception.com';
     $e->to($sendto)
         ->from($sendfrom)
         ->subject($subject);
 });
2021-05-01
  • php

同じタグを持つ記事をピックアップしました。