Skip to content

Instantly share code, notes, and snippets.

@karandatwani92
Last active August 15, 2022 02:02
Show Gist options
  • Save karandatwani92/0b0429026f031d8cfb2c4645c6e3bbca to your computer and use it in GitHub Desktop.
Save karandatwani92/0b0429026f031d8cfb2c4645c6e3bbca to your computer and use it in GitHub Desktop.
@if ($crud->hasAccess('email'))
<a href="{{ url($crud->route.'/'.$entry->getKey().'/email') }}" class="btn btn-sm btn-link"><i class="la la-envelope"></i> Email</a>
@endif
@extends(backpack_view('blank'))
@php
$defaultBreadcrumbs = [
trans('backpack::crud.admin') => url(config('backpack.base.route_prefix'), 'dashboard'),
$crud->entity_name_plural => url($crud->route),
'Email' => false,
];
// if breadcrumbs aren't defined in the CrudController, use the default breadcrumbs
$breadcrumbs = $breadcrumbs ?? $defaultBreadcrumbs;
@endphp
@section('header')
<section class="container-fluid">
<h2>
<span class="text-capitalize">Send Email</span>
<small>Sending email to {!! $entry->name !!}.</small>
@if ($crud->hasAccess('list'))
<small>
<a href="{{ url($crud->route) }}" class="d-print-none font-sm">
<i
class="la la-angle-double-{{ config('backpack.base.html_direction') == 'rtl' ? 'right' : 'left' }}"></i>
{{ trans('backpack::crud.back_to_all') }}
<span>{{ $crud->entity_name_plural }}</span>
</a>
</small>
@endif
</h2>
</section>
@endsection
@section('content')
<div class="row">
<div class="col-md-8 bold-labels">
@if ($errors->any())
<div class="alert alert-danger pb-0">
<ul class="list-unstyled">
@foreach ($errors->all() as $error)
<li><i class="la la-info-circle"></i> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="">
@csrf
<div class="card">
<div class="card-body row">
<div class="form-group col-md-4">
<label>From</label>
<input type="text" name="from" value="{{ old('from',env('MAIL_FROM_ADDRESS')) }}" class="form-control @error('from') is-invalid @enderror">
@error('from')
<div class="invalid-feedback d-block">{{ $message }}</div>
@enderror
</div>
<div class="form-group col-md-4">
<label>To</label>
<input type="text" name="to" value="{{ $entry->email }}" readonly="readonly" disabled="disabled" class="form-control">
</div>
<div class="form-group col-md-4">
<label>Reply To</label>
<input type="text" name="reply_to" value="{{ old('reply_to',backpack_user()->email) }}" class="form-control @error('reply_to') is-invalid @enderror">
@error('reply_to')
<div class="invalid-feedback d-block">{{ $message }}</div>
@enderror
</div>
<div class="form-group col-sm-12">
<label>Subject</label>
<input type="text" name="subject" value="{{ old('subject') }}" class="form-control @error('subject') is-invalid @enderror">
@error('subject')
<div class="invalid-feedback d-block">{{ $message }}</div>
@enderror
</div>
<div class="form-group col-sm-12">
<label>Message</label>
<textarea name="message" class="form-control @error('message') is-invalid @enderror">{{ old('message') }}</textarea>
@error('message')
<div class="invalid-feedback d-block">{{ $message }}</div>
@enderror
</div>
</div>
</div>
<div class="d-none" id="parentLoadedAssets">[]</div>
<div id="saveActions" class="form-group">
<input type="hidden" name="_save_action" value="send_email">
<button type="submit" class="btn btn-success">
<span class="la la-save" role="presentation" aria-hidden="true"></span> &nbsp;
<span data-value="send_email">Send Email</span>
</button>
<div class="btn-group" role="group">
</div>
<a href="{{ url($crud->route) }}" class="btn btn-default"><span class="la la-ban"></span>
&nbsp;Cancel</a>
</div>
</form>
</div>
</div>
@endsection
<?php
namespace App\Http\Controllers\Admin\Operations;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Exception;
use Validator;
use Alert;
trait EmailOperation
{
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupEmailRoutes($segment, $routeName, $controller)
{
Route::get($segment . '/{id}/email', [
'as' => $routeName . '.email',
'uses' => $controller . '@email',
'operation' => 'email',
]);
Route::post($segment . '/{id}/email', [
'as' => $routeName . '.email-send',
'uses' => $controller . '@postEmailForm',
'operation' => 'email',
]);
}
/**
* Add the default settings, buttons, etc that this operation needs.
*/
protected function setupEmailDefaults()
{
$this->crud->allowAccess('email');
$this->crud->operation('email', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
});
$this->crud->operation('list', function () {
// $this->crud->addButton('top', 'email', 'view', 'crud::buttons.email');
$this->crud->addButton('line', 'email', 'view', 'crud::buttons.email');
});
}
/**
* Show the view for performing the operation.
*
* @return Response
*/
public function email()
{
$this->crud->hasAccessOrFail('email');
// prepare the fields you need to show
$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? 'email ' . $this->crud->entity_name;
$this->data['entry'] = $this->crud->getCurrentEntry();
// load the view
return view("crud::operations.email_form", $this->data);
}
public function postEmailForm(Request $request)
{
// run validation
$validator = Validator::make($request->all(), [
'from' => 'required|email',
'reply_to' => 'nullable|email',
'subject' => 'required',
'message' => 'required'
]);
if ($validator->fails())
return redirect()->back()->withErrors($validator)->withInput();
$entry = $this->crud->getCurrentEntry();
try {
// send the actual email
Mail::raw($request['message'], function ($message) use ($entry, $request) {
$message->from($request->from);
$message->replyTo($request->reply_to);
$message->to($entry->email, $entry->name);
$message->subject($request['subject']);
});
Alert::success('Mail Sent')->flash();
return redirect(url($this->crud->route));
} catch (Exception $e) {
// show a bubble with the error message
Alert::error("Error, " . $e->getMessage())->flash();
return redirect()->back()->withInput();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment