diff --git a/src/Jobs/ResendMailJob.php b/src/Jobs/ResendMailJob.php index 0fd2a38..564536f 100644 --- a/src/Jobs/ResendMailJob.php +++ b/src/Jobs/ResendMailJob.php @@ -30,50 +30,48 @@ public function __construct( public function handle(): void { - Mail::send([], callback: function (Message $message) { - $message->html($this->mail->html ?? '') - ->text($this->mail->text ?? ''); - - foreach ($this->mail->attachments as $attachment) { - $message->attachData($attachment->fileData, $attachment->filename, ['mime' => $attachment->mime]); - } - - $message = $message - ->subject($this->mail->subject ?? '') - ->from(address: array_key_first( - $this->getFrom($this->mail->from) - )) - ->to($this->to ?? []); - - if ($this->mail->cc) { - $message->cc($this->mail->cc); - } + Mail::send([], [], function (Message $message) { + $this->setMessageContent($message) + ->setMessageRecipients($message); + }); + } - if ($this->mail->bcc) { - $message->bcc($this->mail->bcc); - } + private function setMessageContent(Message $message): self + { + $message->html($this->mail->html ?? '') + ->text($this->mail->text ?? ''); - if ($this->mail->reply_to) { - $message->replyTo(address: array_key_first( - $this->getFrom($this->mail->reply_to) - )); - } + foreach ($this->mail->attachments as $attachment) { + $message->attachData($attachment->fileData, $attachment->filename, ['mime' => $attachment->mime]); + } - return $message; - }); + return $this; } - private function getFrom(string $from): array + private function setMessageRecipients(Message $message): self { - // Decode the JSON string into an array - $fromArray = json_decode($from, true); + $message->subject($this->mail->subject ?? '') + ->from($this->getFirstAddress($this->mail->from)) + ->to($this->to); + + if ($this->mail->cc) { + $message->cc($this->mail->cc); + } - // Get the first key (email address) - $fromEmail = array_key_first($fromArray); + if ($this->mail->bcc) { + $message->bcc($this->mail->bcc); + } - // Get the value (name) associated with the first key - $fromName = $fromArray[$fromEmail] ?? null; + if ($this->mail->reply_to) { + $message->replyTo($this->getFirstAddress($this->mail->reply_to)); + } - return [$fromEmail => $fromName]; + return $this; + } + + private function getFirstAddress(string $jsonAddresses): string + { + $addresses = json_decode($jsonAddresses, true); + return array_key_first($addresses); } -} +} \ No newline at end of file