Skip to content

Instantly share code, notes, and snippets.

@hansvn
Last active February 16, 2018 19:02
Show Gist options
  • Save hansvn/8f85991f68655c77f6a86cb61202d107 to your computer and use it in GitHub Desktop.
Save hansvn/8f85991f68655c77f6a86cb61202d107 to your computer and use it in GitHub Desktop.
Laravel 5 Error mailer
{{-- file: resources/views/emails/error.blade.php --}}
<?php
//to create one dimensional arrays
if(!function_exists('array_flatten_withkeys')) {
function array_flatten_withkeys($array, &$newArray = array()) {
foreach($array as $key => $child) {
if(is_array($child)) {
$newArray = array_flatten_withkeys($child, $newArray);
} else {
$newArray[$key] = $child;
}
}
return $newArray;
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{ $url }} : Server Error</title>
<style type="text/css">
/* Based on The MailChimp Reset INLINE: Yes. */
/* Client-specific Styles */
#outlook a {padding:0;} /* Force Outlook to provide a "view in browser" menu link. */
body{ -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; }
/* Prevent Webkit and Windows Mobile platforms from changing default font sizes.*/
.ExternalClass {width:100%;} /* Force Hotmail to display emails at full width */
.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;}
/* Forces Hotmail to display normal line spacing. More on that: http://www.emailonacid.com/forum/viewthread/43/ */
#backgroundTable {margin:0; padding:0; width:100% !important; line-height: 100% !important;}
/* End reset */
/* Some sensible defaults for images
Bring inline: Yes. */
img {outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;}
a img {border:none;}
.image_fix {display:block;}
/* Yahoo paragraph fix
Bring inline: Yes. */
p {margin: 1em 0;}
/* Hotmail header color reset
Bring inline: Yes. */
h1, h2, h3, h4, h5, h6 {color: black !important;}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {color: blue !important;}
h1 a:active, h2 a:active, h3 a:active, h4 a:active, h5 a:active, h6 a:active {
color: red !important; /* Preferably not the same color as the normal header link color. There is limited support for psuedo classes in email clients, this was added just for good measure. */
}
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited {
color: purple !important; /* Preferably not the same color as the normal header link color. There is limited support for psuedo classes in email clients, this was added just for good measure. */
}
/* Outlook 07, 10 Padding issue fix
Bring inline: No.*/
table td {border-collapse: collapse;}
/* Remove spacing around Outlook 07, 10 tables
Bring inline: Yes */
table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }
/* Styling your links has become much simpler with the new Yahoo. In fact, it falls in line with the main credo of styling in email and make sure to bring your styles inline. Your link colors will be uniform across clients when brought inline.
Bring inline: Yes. */
a {color: #4787ff;}
/***************************************************
****************************************************
MOBILE TARGETING
****************************************************
***************************************************/
@media only screen and (max-device-width: 480px) {
/* Part one of controlling phone number linking for mobile. */
a[href^="tel"], a[href^="sms"] {
text-decoration: none;
color: blue; /* or whatever your want */
pointer-events: none;
cursor: default;
}
.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
text-decoration: default;
color: #4787ff !important;
pointer-events: auto;
cursor: default;
}
}
/* More Specific Targeting */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
/* You guessed it, ipad (tablets, smaller screens, etc) */
/* repeating for the ipad */
a[href^="tel"], a[href^="sms"] {
text-decoration: none;
color: blue; /* or whatever your want */
pointer-events: none;
cursor: default;
}
.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
text-decoration: default;
color: #4787ff !important;
pointer-events: auto;
cursor: default;
}
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* Put your iPhone 4g styles in here */
}
/* Android targeting */
@media only screen and (-webkit-device-pixel-ratio:.75){
/* Put CSS for low density (ldpi) Android layouts in here */
}
@media only screen and (-webkit-device-pixel-ratio:1){
/* Put CSS for medium density (mdpi) Android layouts in here */
}
@media only screen and (-webkit-device-pixel-ratio:1.5){
/* Put CSS for high density (hdpi) Android layouts in here */
}
/* end Android targeting */
</style>
<!-- Targeting Windows Mobile -->
<!--[if IEMobile 7]>
<style type="text/css">
</style>
<![endif]-->
<!-- ***********************************************
****************************************************
END MOBILE TARGETING
****************************************************
************************************************ -->
<!--[if gte mso 9]>
<style>
/* Target Outlook 2007 and 2010 */
</style>
<![endif]-->
</head>
<body>
<!-- Wrapper/Container Table: Use a wrapper table to control the width and the background color consistently of your email. Use this approach instead of setting attributes on the body tag. -->
<table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
<tr>
<td valign="top">
{{--
<!-- Tables are the most common way to format your email consistently. Set your table widths inside cells and in most cases reset cellpadding, cellspacing, and border to zero. Use nested tables as a way to space effectively in your message. -->
<table cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td width="200" valign="top"></td>
<td width="200" valign="top"></td>
<td width="200" valign="top"></td>
</tr>
</table>
<!-- End example table -->
<!-- Yahoo Link color fix updated: Simply bring your link styling inline. -->
<a href="http://htmlemailboilerplate.com" target ="_blank" title="Styling Links" style="color: #4787ff; text-decoration: none;">Coloring Links appropriately</a>
<!-- Gmail/Hotmail image display fix -->
<img class="image_fix" src="full path to image" alt="Your alt text" title="Your title text" width="x" height="x" />
<!-- Working with telephone numbers (including sms prompts). Use the "mobile" class to style appropriately in desktop clients
versus mobile clients. -->
<span class="mobile_link">123-456-7890</span>
--}}
<table cellpadding="0" cellspacing="0" border="0" align="left" width="100%">
<tr>
<td valign="top">
{!! $exception_trace !!}
<br />
Status Code: {{$status_code}}
<h2>PHP environment:</h2>
<pre style="max-width:600px;">
{-- do not expose .env data! --}
$_SERVER = {{ print_r(array_diff(array_flatten_withkeys($_SERVER), array_flatten_withkeys($_ENV)), true) }}
$_REQUEST = {{ print_r($request, true) }}
@if(Auth::check())
Auth::user() = {{Auth::user()->name}} ({{Auth::id()}})
@endif
</pre>
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" align="center" width="1" height="100">
<tr>
<td width="1" height="100" valign="top"></td>
</tr>
</table>
</td>
</tr>
</table>
<!-- End of wrapper table -->
</body>
</html>
<?php
// file: app/Exceptions/Handler.php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
// send error mail with trace:
if(!config('app.debug') && parent::shouldReport($exception))
{
$fullUrl = $request->fullUrl();
$flattened = \Symfony\Component\Debug\Exception\FlattenException::create($exception);
$handler = new \Symfony\Component\Debug\ExceptionHandler();
$trace = $handler->getContent($flattened);
// request content to build the mail from
$request_content = array(
'url' => $request->url(),
'secure' => $request->secure() ? 'yes' : 'no',
'method' => $request->getMethod(),
'input' => $request->except($this->dontFlash),// do not expose 'sensitive' data
'ajax' => $request->ajax() ? 'yes' : 'no',
);
// derive the error code
if(method_exists($exception, 'getStatusCode')) {
$status_code = $exception->getStatusCode() != 0 ? $exception->getStatusCode() : $exception->getCode();
} elseif(method_exists($exception, 'getCode')) {
$status_code = $exception->getCode();
} else {
$status_code = 'undefined';
}
// send the mail (consider Mail::send if no queue driver is configured)
\Mail::queue('emails.error', ['url' => $fullUrl, 'request' => $request_content, 'exception_trace' => $trace, 'status_code' => $status_code], function($message) use ($fullUrl, $status_code) {
$message->to('<Your Error Reporting Inbox>', '<Name of the inbox>')->subject("$fullUrl : Server Error ($status_code)");
});
}
// continue Laravels error reporting
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment