Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Mailing / Suspend System #985

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function index()
'value' => $value,
'label' => $optionInputData[$key]['label'] ?? ucwords(str_replace('_', ' ', $key)),
'type' => $optionInputData[$key]['type'] ?? 'string',
'tooltip' => $optionInputData[$key]['tooltip'] ?? '',
'description' => $optionInputData[$key]['description'] ?? '',
'options' => $optionInputData[$key]['options'] ?? [],
'identifier' => $optionInputData[$key]['identifier'] ?? 'option'
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ private function serverCreationFailed(Response $response, Server $server)
/** Remove the specified resource from storage. */
public function destroy(Server $server)
{
if ($server->user_id != Auth::user()->id) {
return back()->with('error', __('This is not your Server!'));
}
try {
$server->delete();

Expand Down
49 changes: 49 additions & 0 deletions app/Mail/TestMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
use Queueable, SerializesModels;

protected array $template = ['subject' => '', 'body' => ''];
protected array $variables;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($template, $variables)
{
$this->template = $template;
$this->variables = $variables;
}

/**
* Build the message.
*
* @return $this
*/
public function build(): static
{
$subject = $this->replaceVariables($this->template['subject'], $this->variables);
$body = $this->replaceVariables($this->template['body'], $this->variables);

return $this->markdown('mail.test')
->subject($subject)
->with('body', $body);
}

protected function replaceVariables($content, $variables)
{
foreach ($variables as $key => $value) {
$content = str_replace("{" . $key . "}", $value, $content);
}
return $content;
}
}
29 changes: 29 additions & 0 deletions app/Services/MailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Services;

use App\Mail\TestMail;
use App\Settings\MailTemplatesSettings;
use Illuminate\Support\Facades\Mail;

class MailService
{
public function sendTestMail($user): void
{
$mailTemplatesSettings = (new MailTemplatesSettings());
// Fetch the mail template from the database
$template = [
'subject' => $mailTemplatesSettings->mail_welcome_subject,
'body' => $mailTemplatesSettings->mail_welcome_body,
];

// Prepare the variables to replace in the template
$variables = [
'panel' => env('APP_NAME'),
'user' => $user->name,
];

// Send the mail
Mail::to($user->email)->send(new TestMail($template, $variables));
}
}
51 changes: 51 additions & 0 deletions app/Settings/MailTemplatesSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Settings;

use Spatie\LaravelSettings\Settings;

class MailTemplatesSettings extends Settings
{
public ?string $mail_welcome_subject;
public ?string $mail_welcome_body;

public static function group(): string
{
return 'templates';
}

/**
* Summary of validations array
* @return array<string, string>
*/
public static function getValidations()
{
return [
'mail_welcome_subject' => 'nullable|string',
'mail_welcome_body' => 'nullable|string',
];
}

/**
* Summary of optionTypes
* Only used for the settings page
* @return array<array<'type'|'label'|'description'|'options', string|bool|float|int|array<string, string>>>
*/
public static function getOptionInputData()
{
return [
'category_icon' => 'fas fa-envelope',
'mail_welcome_subject' => [
'label' => 'Welcome Mail Subject',
'type' => 'string',
'description' => 'The subject of the welcome mail.',
],
'mail_welcome_body' => [
'label' => 'Welcome Mail Body',
'type' => 'textarea',
'description' => 'The body of the welcome mail.',
'tooltip' => 'You can use {panel} and {user} as placeholders. These variables will be replaced with Panel name and User name respectively.',
],
];
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,

/*
* Package Service Providers...
*/

/*
* Application Service Providers...
*/
App\Providers\MailServiceProvider::class,
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
Expand Down
2 changes: 2 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Settings\InvoiceSettings;
use App\Settings\LocaleSettings;
use App\Settings\MailSettings;
use App\Settings\MailTemplatesSettings;
use App\Settings\PterodactylSettings;
use App\Settings\ReferralSettings;
use App\Settings\ServerSettings;
Expand All @@ -26,6 +27,7 @@
InvoiceSettings::class,
LocaleSettings::class,
MailSettings::class,
MailTemplatesSettings::class,
PterodactylSettings::class,
ReferralSettings::class,
ServerSettings::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

class CreateMailLayoutSettings extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('templates.mail_welcome_subject', 'Welcome to {panel}!');
$this->migrator->add('templates.mail_welcome_body', 'Hello {user}, welcome to {panel}!');
}

public function down(): void
{
$this->migrator->delete('templates.mail_welcome_subject');
$this->migrator->delete('templates.mail_welcome_body');
}
}
13 changes: 11 additions & 2 deletions themes/default/views/admin/settings/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ class="custom-select w-100"
@break

@case($value['type'] == 'textarea')
<textarea class="form-control" name="{{ $key }}" rows="3">{{ $value['value'] }}</textarea>
<!-- hide the textarea and use a button to reopen it -->
<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#{{ $key }}Collapse">
{{ __('Edit') }}
</button><br />
<div id="{{ $key }}Collapse" class="collapse">
<textarea class="form-control" name="{{ $key }}" rows="3">{{ $value['value'] }}</textarea>
@if ($value['tooltip'])
<small class="text-muted">{{ $value['tooltip'] }}</small>
@endif
</div>
@break

@default
Expand Down Expand Up @@ -349,7 +358,7 @@ class="float-right ml-2 btn btn-secondary">Reset
skin: "oxide-dark",
content_css: "dark",
branding: false,
height: 500,
height: 350,
width: '100%',
plugins: ['image', 'link'],
});
Expand Down
7 changes: 7 additions & 0 deletions themes/default/views/mail/test.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@component('mail::message')
{!! $body !!}

<br>
{{__('Thanks')}},<br>
{{ config('app.name') }}
@endcomponent