Skip to content

Commit

Permalink
Added fallback methods for from mails
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMoustache committed Oct 17, 2023
1 parent a089645 commit d771816
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Facades/MailTemplateFallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Codedor\FilamentMailTemplates\Facades;

class MailTemplateFallbacks extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return \Codedor\FilamentMailTemplates\MailTemplateFallbacks::class;
}
}
15 changes: 15 additions & 0 deletions src/Filament/Resources/MailTemplateResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codedor\FilamentMailTemplates\Filament\Resources;

use Codedor\FilamentMailTemplates\Facades\MailTemplateFallbacks;
use Codedor\FilamentMailTemplates\Filament\Resources\MailTemplateResource\Pages;
use Codedor\FilamentMailTemplates\Models\MailTemplate;
use Codedor\FilamentPlaceholderInput\Filament\Forms\Components\PlaceholderInput;
Expand Down Expand Up @@ -60,6 +61,20 @@ public static function form(Form $form): Form
Placeholder::make('description')
->content(fn (MailTemplate $record) => $record->description),

Grid::make()->schema([
TextInput::make('from_name')
->helperText(
'If left empty, the sites default name will be used: ' .
MailTemplateFallbacks::getFromName()
),

TextInput::make('from_email')
->helperText(
'If left empty, the sites default mail will be used: ' .
MailTemplateFallbacks::getFromMail()
),
]),

Repeater::make('to_email')
->helperText('If left empty, the sites default e-mail will be used.')
->label('Target e-mails')
Expand Down
5 changes: 3 additions & 2 deletions src/Mail/MailableTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codedor\FilamentMailTemplates\Mail;

use Codedor\FilamentMailTemplates\Facades\MailTemplateFallbacks;
use Codedor\FilamentMailTemplates\MailTemplateBuilder;
use Codedor\FilamentMailTemplates\Models\MailHistory;
use Codedor\FilamentMailTemplates\Models\MailTemplate;
Expand Down Expand Up @@ -43,8 +44,8 @@ public function envelope(): Envelope
return new Envelope(
subject: $this->parseVariables($this->template->subject),
from: new Address(
$this->template->from_email ?? config('mail.from.address'),
$this->template->from_name ?? config('mail.from.name'),
$this->template->from_email ?? MailTemplateFallbacks::getFromMail(),
$this->template->from_name ?? MailTemplateFallbacks::getFromName(),
),
);
}
Expand Down
39 changes: 39 additions & 0 deletions src/MailTemplateFallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Codedor\FilamentMailTemplates;

class MailTemplateFallbacks
{
public null|string $fromName = null;
public null|string $fromMail = null;

public function __construct()
{
$this->fromName = config('mail.from.name');
$this->fromMail = config('mail.from.address');
}

public function fromName(null|string $fromName): self
{
$this->fromName = $fromName;

return $this;
}

public function getFromName(): null|string
{
return $this->fromName;
}

public function fromMail(null|string $fromMail): self
{
$this->fromMail = $fromMail;

return $this;
}

public function getFromMail(): null|string
{
return $this->fromMail;
}
}

0 comments on commit d771816

Please sign in to comment.