Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Sep 8, 2024
1 parent a20c27c commit 6c12cd0
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/Jobs/ResendMailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 6c12cd0

Please sign in to comment.