You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.1 KiB
70 lines
2.1 KiB
<?php |
|
|
|
namespace App\Services; |
|
|
|
use App\Mail\TemplatedMail; |
|
use Illuminate\Support\Facades\Mail; |
|
|
|
class MailDeliveryService |
|
{ |
|
public function __construct( |
|
private readonly MailSettingsService $mailSettings, |
|
private readonly EmailTemplateService $templates |
|
) { |
|
} |
|
|
|
public function sendTemplated(string $to, string $templateSlug, array $variables): void |
|
{ |
|
$this->mailSettings->apply(); |
|
$rendered = $this->templates->render($templateSlug, $variables); |
|
|
|
Mail::to($to)->send(new TemplatedMail($rendered['subject'], $rendered['html'])); |
|
} |
|
|
|
public function sendTemplatePreview(string $to, string $templateSlug): void |
|
{ |
|
$variables = $this->templates->sampleVariables($templateSlug); |
|
$this->sendTemplated($to, $templateSlug, $variables); |
|
} |
|
|
|
public function sendTest(string $to): void |
|
{ |
|
$this->mailSettings->apply(); |
|
$settings = $this->mailSettings->get(); |
|
|
|
Mail::raw( |
|
'Test email dari ' . config('app.name') . ' pada ' . now()->format('Y-m-d H:i:s') . ' WIB.', |
|
function ($message) use ($to, $settings) { |
|
$message->to($to) |
|
->subject('Test SMTP — ' . config('app.name')) |
|
->from($settings['mail_from_address'], $settings['mail_from_name']); |
|
} |
|
); |
|
} |
|
|
|
/** |
|
* @param list<string> $recipients |
|
* @return list<string> |
|
*/ |
|
public function sendTestToMany(array $recipients): array |
|
{ |
|
$sent = []; |
|
|
|
foreach (array_unique(array_filter(array_map('trim', $recipients))) as $to) { |
|
$this->sendTest($to); |
|
$sent[] = $to; |
|
} |
|
|
|
return $sent; |
|
} |
|
|
|
public function notifyPasswordChanged(string $email, string $username, string $displayName, ?string $changedBy = null): void |
|
{ |
|
$this->sendTemplated($email, 'password_changed', [ |
|
'username' => $username, |
|
'display_name' => $displayName !== '' ? $displayName : $username, |
|
'changed_at' => now()->format('Y-m-d H:i:s') . ' WIB', |
|
'changed_by' => $changedBy ?? 'system', |
|
]); |
|
} |
|
}
|
|
|