templateId = $templateId; $this->sender( config('mail_templates.sender.email'), config('mail_templates.sender.name') ); } /** * @param string $address * @param string|null $name * @return MailTemplate */ public function sender(string $address, string $name = null): MailTemplate { $this->sender['email'] = $address; if (! empty($name)) $this->sender['name'] = $name; return $this; } /** * @param string $address * @param string|null $name * @return MailTemplate */ public function replyTo(string $address, string $name = null): MailTemplate { $this->replyTo['email'] = $address; if (! empty($name)) $this->replyTo['name'] = $name; return $this; } /** * @param array $recipients * @return MailTemplate */ public function recipients(array $recipients): MailTemplate { $this->recipients = []; if (! empty($recipients)) { foreach ($recipients as $recipient) { if (!empty($recipient[0]) && is_string($recipient[0])) { $data = [ 'email' => $recipient[0] ]; if (!empty($recipient[1])) { if (!empty($recipient[1]['name'])) $data['name'] = $recipient[1]['name']; if (!empty($recipient[1]['parameters'])) $data['parameters'] = $recipient[1]['parameters']; } $this->recipients[] = $data; } } } return $this; } /** * @param string $address * @param array $informations * @return MailTemplate */ public function addRecipient(string $address, array $informations = []): MailTemplate { $data = [ 'email' => $address ]; if (!empty($informations)) { if (!empty($informations['name'])) $data['name'] = $informations['name']; if (!empty($informations['parameters'])) $data['parameters'] = $informations['parameters']; } $this->recipients[] = $data; return $this; } /** * @param array $parameters * @return MailTemplate */ public function parameters(array $parameters): MailTemplate { $this->parameters = $parameters; return $this; } /** * @param array $attachments * @return MailTemplate */ public function attachments(array $attachments): MailTemplate { foreach ($attachments as $attachment) { $this->addAttachment($attachment['url'] ?? $attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null); } return $this; } public function addAttachment($file, $filename = null) { $file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file; $this->attachments[] = [ 'path' => $file, 'filename' => $filename ?? basename($file) ]; return $this; } /** * @param string $key * @param string $value * @return MailTemplate */ public function addParameter(string $key, string $value): MailTemplate { $this->parameters[$key] = $value; return $this; } /** * Get an array representation of the message. * * @return array */ public function toArray(): array { return [ 'templateId' => $this->templateId, 'sender' => $this->sender, 'replyTo' => $this->replyTo, 'recipients' => $this->recipients, 'attachments' => $this->attachments, 'parameters' => $this->parameters, ]; } public function jsonSerialize(): mixed { return $this->toArray(); } }