Updated Mailer (#113)

Removed email settings from settings.json and moved them to the .env
file to use Laravel's mailer functionality.

references to the old mailer has been removed from settings.json,
data sorting class and the settings template file

the front end script has also been updated to accomodate different
message types, starting with a test message so members can make sure the
settings defined in the .env are working
This commit is contained in:
ro 2024-06-05 13:33:11 -06:00
parent 9923f087a6
commit b37e64d062
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
11 changed files with 134 additions and 82 deletions

View file

@ -0,0 +1,35 @@
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\SystemEmail;
class MailAPIController extends Controller
{
public function __construct()
{
}
//init stuff
public function sendNotify(Request $request)
{
$result = [];
try {
Mail::to(env('ADMIN_EMAIL'))->send(new SystemEmail($request->content));
$result = [
'type' => 'mail_good',
'message' => 'Mail Sent',
];
} catch (ERROR $e) {
$result = [
'type' => 'mail_not_good',
'message' => 'Mail Not Sent',
];
}
return response()->json($result)->header('Content-Type', 'application/json');
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Mail;
class SystemMailController extends Controller
{
/**
* Send notification
*/
public function sendNotification()
{
$message = "This is something important. Probably";
Mail::to(env('ADMIN_EMAIL'))->send(new SystemEmail($message));
}
}

55
app/Mail/SystemEmail.php Normal file
View file

@ -0,0 +1,55 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Queue\SerializesModels;
class SystemEmail extends Mailable
{
use Queueable;
use SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(public $notifyText)
{
//
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Address(env('MAIL_USERNAME'), 'System Notification'),
subject: 'Notification',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.notify',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

View file

@ -265,7 +265,6 @@ class SortingService
public function settings()
{
$global = $this->settings->getGlobal();
$email = $this->settings->getEmail();
$updated = new Carbon($global['last_backup']);
$status = session('member') != '' ? true : false;
$pageOptions = [
@ -283,8 +282,6 @@ class SortingService
'themes' => $this->themes->getThemes(),
'apiStatus' => isset($global['externalAPI']) ? $global['externalAPI'] : 'false',
'dynamicRenderStatus' => isset($global['dynamicRender']) ? $global['dynamicRender'] : 'false',
'mailOption' => $email['active'],
'mailConfig' => $email,
'status' => $status,
];
return $pageOptions;

View file

@ -15,17 +15,5 @@
"library_stats": {
"current_index": 1
},
"email": {
"active": "none",
"smtp": {
"domain": "",
"email": "",
"password": ""
},
"mailgun": {
"domain": "",
"api-key": ""
}
},
"menu": []
}

View file

@ -149,6 +149,7 @@ section#site-features > div.features-mail {
border-radius: 3px;
background: var(--secondary);
padding: 10px;
color: var(--primary);
}
section#site-features > div.features-mail input {

View file

@ -9,24 +9,16 @@ export default class Mailer {
//--------------------------
// methods
//--------------------------
sendMail() {
let mailData = {
content: 'This is a test email'
};
let admin = new FipamoAdminAPI();
admin
.sendMail(mailData)
.then(result => {
notify.alert(result.message, true);
})
.catch(err => {
notify.alert(err.message, false);
});
sendMail(task = null, content = null) {
let text = '';
if (task == 'TEST') {
text = 'This is a test email';
} else {
text = content;
}
testMail() {
let mailData = {
content: 'This is a test email',
mail_task: 'TESTING'
content: text,
mail_task: task
};
let admin = new FipamoAdminAPI();
admin

View file

@ -110,8 +110,8 @@ export default class SettingsIndex {
}
document
.getElementById('send-mail')
.addEventListener('click', e => this.handleMailer(e));
.getElementById('send-test-mail')
.addEventListener('click', e => this.handleTestMail(e));
document
.getElementById('publish-pages')
.addEventListener('click', e => this.handlePublished(e));
@ -160,10 +160,10 @@ export default class SettingsIndex {
//e.target.innerHTML = "DON'T RENDER PAGES ON SAVE";
}
}
handleMailer() {
handleTestMail() {
let mailer = new Mailer();
mailer.testMail();
//mailer.sendMail();
//mailer.testMail();
mailer.sendMail('TEST');
}
handleThemes(e) {
e.stopPropagation();
@ -185,30 +185,6 @@ export default class SettingsIndex {
}
}
}
handleMailOptions(e) {
e.preventDefault();
e.stopPropagation();
let smtp = document.getElementById('mail-smtp');
let mailgun = document.getElementById('mail-mg');
let mail = document.querySelectorAll('.mail-option');
for (var i = 0, length = mail.length; i < length; i++) {
if (e.target.id == mail[i].id) {
mail[i].setAttribute('data-enabled', 'true');
if (e.target.id == 'option-smtp') {
smtp.setAttribute('data-enabled', 'true');
mailgun.setAttribute('data-enabled', 'false');
} else if (e.target.id == 'option-none') {
smtp.setAttribute('data-enabled', 'false');
mailgun.setAttribute('data-enabled', 'false');
} else {
smtp.setAttribute('data-enabled', 'false');
mailgun.setAttribute('data-enabled', 'true');
}
} else {
mail[i].setAttribute('data-enabled', 'false');
}
}
}
handleImageUpload(type, files) {
notify.alert('Uploading Image... ', null);
let self = this;

View file

@ -45,32 +45,16 @@
</section>
<section id="site-features" class="section-tab hide">
<div class="features-mail">
<label>SYSTEM EMAIL</label>
<div>
@if($mailOption == "option-none" or $mailOption == "")
<a href="#" class="mail-option" id="option-none" data-enabled="true">NONE</a>
@else
<a href="#" class="mail-option" id="option-none" data-enabled="false">NONE</a>
@endif
@if($mailOption == "option-mg" or $mailOption == "")
<a href="#" class="mail-option" id="option-mg" data-enabled="true">MAILGUN</a>
@else
<a href="#" class="mail-option" id="option-mg" data-enabled="false">MAILGUN</a>
@endif
@if($mailOption == "option-smtp" or $mailOption == "")
<a href="#" class="mail-option" id="option-smtp" data-enabled="true">SMTP</a>
@else
<a href="#" class="mail-option" id="option-smtp" data-enabled="false">SMTP</a>
@endif
</div>
@include('forms.mailforms')
<button id="send-mail">
<button id="send-test-mail">
<svg id="nav-menu-icon" class="icon">
<use id="nav-menu-icon" xlink:href="/assets/images/global/sprite.svg#entypo-mail-with-circle"/>
</svg>
<span>TEST MAIL</span>
</button>
<div>
<label>SYSTEM EMAIL</label><br />
set email settings in .env file
</div>
</div>
<div class="site-options">
<div class="option-container">

View file

@ -0,0 +1,4 @@
<div>
<strong>NOTICE</strong><br />
{{$notifyText}}<br />
</div>

View file

@ -6,6 +6,7 @@ use App\Http\Controllers\API\PageAPIController;
use App\Http\Controllers\API\FileUploadAPIController;
use App\Http\Controllers\API\SettingsAPIController;
use App\Http\Controllers\API\InitAPIController;
use App\Http\Controllers\API\MailAPIController;
/*
|--------------------------------------------------------------------------
@ -34,3 +35,5 @@ Route::get("/v1/backup/download", [SettingsAPIController::class, 'downloadBackup
//init
Route::post("/v1/init", [InitAPIController::class, 'setupFresh']);
Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
//mail
Route::post("/v1/mailer", [MailAPIController::class, 'sendNotify']);