Skip to content

Commit 0fe4cd9

Browse files
authored
Merge pull request #46 from uchajk/email-to
feat: Implement optional email results
2 parents 4e7cbcb + a9bbd25 commit 0fe4cd9

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/Jobs/DataTableExportJob.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Illuminate\Support\Collection;
1818
use Illuminate\Support\Facades\Auth;
1919
use Illuminate\Support\Facades\Event;
20+
use Illuminate\Support\Facades\Mail;
2021
use Illuminate\Support\Facades\Storage;
2122
use Illuminate\Support\Str;
2223
use OpenSpout\Common\Helper\CellTypeHelper;
@@ -183,6 +184,11 @@ public function handle()
183184
if ($this->getS3Disk()) {
184185
Storage::disk($this->getS3Disk())->putFileAs('', (new File($path)), $filename);
185186
}
187+
188+
if (request('emailTo')) {
189+
$data = ['email' => urldecode(strval(request('emailTo'))), 'path' => $path];
190+
$this->sendResults($data);
191+
}
186192
}
187193

188194
/**
@@ -271,4 +277,18 @@ protected function isNumeric($value): bool
271277

272278
return is_numeric($value);
273279
}
280+
281+
/**
282+
* @param array $data
283+
* @return void
284+
*/
285+
public function sendResults(array $data): void
286+
{
287+
Mail::send('datatables-export::export-email', $data, function ($message) use ($data) {
288+
$message->attach($data['path']);
289+
$message->to($data['email'])
290+
->subject('Export Report');
291+
$message->from(config('datatables-export.mail_from'));
292+
});
293+
}
274294
}

src/Livewire/ExportButtonComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ExportButtonComponent extends Component
1919

2020
public ?string $tableId;
2121

22+
public ?string $emailTo = '';
23+
2224
public string $type = 'xlsx';
2325

2426
public string $filename = '';

src/config/datatables-export.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
*/
4949
's3_disk' => '',
5050

51+
/*
52+
|--------------------------------------------------------------------------
53+
| Mail from address
54+
|--------------------------------------------------------------------------
55+
|
56+
| Will be used to email report from this address.
57+
|
58+
*/
59+
'mail_from' => env('MAIL_FROM_ADDRESS', ''),
60+
5161
/*
5262
|--------------------------------------------------------------------------
5363
| Default Date Format

src/resources/views/export-button.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
x-on:submit.prevent="
44
$refs.exportBtn.disabled = true;
55
var url = window._buildUrl(LaravelDataTables['{{ $tableId }}'], 'exportQueue');
6-
$.get(url + '&exportType={{$fileType}}&sheetName={{$sheetName}}').then(function(exportId) {
6+
$.get(url + '&exportType={{$fileType}}&sheetName={{$sheetName}}&emailTo={{urlencode($emailTo)}}').then(function(exportId) {
77
$wire.export(exportId)
88
}).catch(function(error) {
99
$wire.exportFinished = true;
@@ -20,6 +20,10 @@ class="{{ $class }}"
2020
</button>
2121
</form>
2222

23+
@if($exporting && $emailTo)
24+
<div class="d-inline">Export will be emailed to {{ $emailTo }}.</div>
25+
@endif
26+
2327
@if($exporting && !$exportFinished)
2428
<div class="d-inline" wire:poll="updateExportProgress">Exporting...please wait.</div>
2529
@endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Attached you will find requested report.

0 commit comments

Comments
 (0)