feat: add resend as transactional emails

This commit is contained in:
Andras Bacsai
2023-08-31 13:10:39 +02:00
parent 87dd819ae4
commit 2538890b52
6 changed files with 200 additions and 75 deletions

View File

@@ -6,10 +6,10 @@ use Exception;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class EmailChannel
{
private bool $isResend = false;
public function send(SendsEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
@@ -20,29 +20,52 @@ class EmailChannel
}
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($notifiable, 'smtp_from_address'),
data_get($notifiable, 'smtp_from_name'),
)
->bcc($recepients)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
if ($this->isResend) {
foreach($recepients as $receipient) {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($notifiable, 'smtp_from_address'),
data_get($notifiable, 'smtp_from_name'),
)
->to($receipient)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
} else {
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($notifiable, 'smtp_from_address'),
data_get($notifiable, 'smtp_from_name'),
)
->bcc($recepients)
->subject($mailMessage->subject)
->html((string)$mailMessage->render())
);
}
}
private function bootConfigs($notifiable): void
{
$password = data_get($notifiable, 'smtp_password');
if ($password) $password = decrypt($password);
if (Str::contains(data_get($notifiable, 'smtp_host'),'resend.com')) {
config()->set('mail.default', 'resend');
config()->set('resend.api_key', $password);
} else {
if (data_get($notifiable, 'resend_enabled')) {
$resendAPIKey = data_get($notifiable, 'resend_api_key');
if ($resendAPIKey) {
$this->isResend = true;
config()->set('mail.default', 'resend');
config()->set('resend.api_key', $resendAPIKey);
}
}
if (data_get($notifiable, 'smtp_enabled')) {
$password = data_get($notifiable, 'smtp_password');
if ($password) $password = decrypt($password);
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",