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

Add third party mailer setting and config bootstrapper #86

Merged
merged 5 commits into from
Apr 27, 2024
Merged
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
26 changes: 26 additions & 0 deletions app/Actions/Setting/MailerSettingTestAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Actions\Setting;

use App\Http\Requests\AdminApi\Setting\MailerSettingTestRequest;
use App\Mail\Setting\MailerTest;
use App\Traits\Base\ManagesTenancyMailer;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Mail;

class MailerSettingTestAction
{
use ManagesTenancyMailer;

public function handle(MailerSettingTestRequest $request): void
{
$mailerSettings = Arr::except($request->validated(), 'recipient');

$this->setMailConfigForTenant(tenant(), $mailerSettings);

Mail::to($request->get('recipient'))
->send(new MailerTest($mailerSettings));

$this->setMailConfigDefaults();
}
}
9 changes: 7 additions & 2 deletions app/Bootstrappers/ConfigTenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

namespace App\Bootstrappers;

use App\Traits\Base\ManagesTenancyMailer;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\App;
use Spatie\LaravelSettings\Exceptions\MissingSettings;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

class ConfigTenancyBootstrapper implements TenancyBootstrapper
{
use ManagesTenancyMailer;

public function bootstrap(Tenant $tenant)
{
try {
App::setLocale($tenant->locale);
} catch (MissingSettings $e) {
$this->setMailConfigForTenant($tenant);
} catch (QueryException | MissingSettings $e) {
// For initial migration of setting, this may not be set. No need to fail
}
}

public function revert()
{
App::setLocale(config('app.landlord.locale'));
// Don't need to do anything here as there is no reverting in a single request to worry about
}

}
157 changes: 157 additions & 0 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/**
* This should be used instead of adding config options directly to config/mail.php
* As it allows for defaults to be loaded reloaded if needed
*/
if (! function_exists('default_mail_config')) {
function default_mail_config(): array
{
return [

/*
|--------------------------------------------------------------------------
| Default Mailer
|--------------------------------------------------------------------------
|
| This option controls the default mailer that is used to send any email
| messages sent by your application. Alternative mailers may be setup
| and used as needed; however, this mailer will be used by default.
|
*/

'default' => env('MAIL_MAILER', 'smtp'),

/*
|--------------------------------------------------------------------------
| Mailer Configurations
|--------------------------------------------------------------------------
|
| Here you may configure all of the mailers used by your application plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers to be used while
| sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required.
|
| Supported: "smtp", "sendmail", "mailgun", "ses",
| "postmark", "log", "array"
|
*/

'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
],

'ses' => [
'transport' => 'ses',
],

'mailgun' => [
'transport' => 'mailgun',
],

'postmark' => [
'transport' => 'postmark',
],

'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],

'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],

'array' => [
'transport' => 'array',
],
],

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],

/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/

'markdown' => [
'theme' => 'default',

'paths' => [
resource_path('views/vendor/mail'),
],
],

];
}
}

/**
* This should be used instead of adding config options directly to config/mail.php
* As it allows for defaults to be loaded reloaded if needed
*/
if (! function_exists('default_services_config')) {
function default_services_config(): array
{
return [

/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],

'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],

'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Controllers/AdminApi/Settings/MailerTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers\AdminApi\Settings;

use App\Actions\Setting\MailerSettingTestAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminApi\Setting\MailerSettingTestRequest;
use App\Interfaces\PermissionInterface;

class MailerTestController extends Controller
{
public function __construct()
{
$this->middleware(
PermissionInterface::getMiddlewareString(PermissionInterface::EDIT_SETTINGS)
);
}

public function __invoke(MailerSettingTestRequest $request)
{
app(MailerSettingTestAction::class)->handle($request);
return response()->json([
'message' => 'Test email sent.'
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function rules() : array
{
return [
'gtm_id' => 'nullable|string',
'mailer' => 'nullable|array',
'stripe_publishable_key' => 'nullable|string',
'stripe_secret_key' => 'nullable|string',
];
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/AdminApi/Setting/MailerSettingTestRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests\AdminApi\Setting;

use App\Http\Requests\BaseRequest;
use App\Interfaces\ThirdPartyInterface;
use Illuminate\Validation\Rule;

class MailerSettingTestRequest extends BaseRequest
{
public function rules(): array
{
return [
'recipient' => 'required|email',
'mailer' => [
'required',
Rule::in(array_keys(ThirdPartyInterface::ALL_MAILERS_LABELLED))
],
ThirdPartyInterface::MAILER_CONFIG_ENCRYPTION => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_FROM_ADDRESS => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_FROM_NAME => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_HOST => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_USERNAME => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_PASSWORD => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_PORT => 'sometimes|nullable|numeric',
ThirdPartyInterface::MAILER_CONFIG_SERVICE_DOMAIN => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_SERVICE_ENDPOINT => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_SERVICE_KEY => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_SERVICE_REGION => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_SERVICE_SECRET => 'sometimes|nullable|string',
ThirdPartyInterface::MAILER_CONFIG_TIMEOUT => 'sometimes|nullable|integer',

];
}
}
11 changes: 11 additions & 0 deletions app/Http/Resources/Admin/Setting/ThirdPartySettingEditResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Resources\Admin\Setting;

use App\Interfaces\ThirdPartyInterface;
use Illuminate\Http\Resources\Json\JsonResource;

class ThirdPartySettingEditResource extends JsonResource
Expand All @@ -27,6 +28,16 @@ public function toArray($request)
'label' => 'GTM ID',
'description' => 'The id for Google Tag Manager. Enables GTM if set.',
],
'mailer' => [
'value' => $this->mailer,
'type' => 'mailer',
'label' => 'Mail Settings',
'description' => 'The config for the mailer.',
'mailers' => ThirdPartyInterface::ALL_MAILERS_LABELLED,
'mailerSettingKeys' => ThirdPartyInterface::ALL_MAILER_SETTING_KEYS,
'mailerSettingLabels' => ThirdPartyInterface::ALL_MAILER_CONFIGS_LABELLED,
'mailerSettingTypes' => ThirdPartyInterface::ALL_MAILER_CONFIGS_TYPES,
],
];
}
}
2 changes: 0 additions & 2 deletions app/Interfaces/ThemeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Interfaces;

use Illuminate\Support\Arr;

class ThemeInterface {
const COLOR_BASE = 'COLOR_BASE';
const COLOR_BASE_CONTRAST = 'COLOR_BASE_CONTRAST';
Expand Down
Loading