|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Modules\Core\Mail; |
| 6 | + |
| 7 | +use Illuminate\Bus\Queueable; |
| 8 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | +use Illuminate\Mail\Mailable; |
| 10 | +use Illuminate\Mail\Mailables\Address; |
| 11 | +use Illuminate\Mail\Mailables\Content; |
| 12 | +use Illuminate\Mail\Mailables\Envelope; |
| 13 | +use Illuminate\Queue\SerializesModels; |
| 14 | +use Modules\Core\Models\EmailLog; |
| 15 | +use Modules\Core\Models\EmailTemplate; |
| 16 | + |
| 17 | +class VertexDynamicMail extends Mailable implements ShouldQueue |
| 18 | +{ |
| 19 | + use Queueable, SerializesModels; |
| 20 | + |
| 21 | + public int $tries = 3; |
| 22 | + |
| 23 | + public string $templateKey; |
| 24 | + |
| 25 | + public string $recipientEmail = ''; |
| 26 | + |
| 27 | + protected string $resolvedSubject; |
| 28 | + |
| 29 | + protected string $renderedBody; |
| 30 | + |
| 31 | + protected bool $isPlain = false; |
| 32 | + |
| 33 | + public function __construct(string $templateKey, array $variables = [], ?string $recipientEmail = null) |
| 34 | + { |
| 35 | + $this->templateKey = $templateKey; |
| 36 | + $this->recipientEmail = $recipientEmail ?? ''; |
| 37 | + |
| 38 | + $template = EmailTemplate::where('key', $templateKey)->firstOrFail(); |
| 39 | + $this->resolvedSubject = $template->subject; |
| 40 | + $this->isPlain = ! ($template->is_html ?? true); |
| 41 | + |
| 42 | + $content = $this->substituteVariables($template->content_html ?? '', $variables); |
| 43 | + $this->renderedBody = $this->isPlain ? nl2br(e($content)) : $content; |
| 44 | + |
| 45 | + $fromAddress = setting('mail_from_address', config('mail.from.address')); |
| 46 | + $entityRefId = 'mail_' . uniqid('', true); |
| 47 | + $this->withSymfonyMessage(new AddVertexMailHeaders($fromAddress, $entityRefId, $this->templateKey)); |
| 48 | + } |
| 49 | + |
| 50 | + public function envelope(): Envelope |
| 51 | + { |
| 52 | + $fromAddress = setting('mail_from_address', config('mail.from.address')); |
| 53 | + $fromName = setting('mail_from_name', config('mail.from.name')); |
| 54 | + $from = new Address($fromAddress, $fromName); |
| 55 | + |
| 56 | + return new Envelope( |
| 57 | + subject: $this->resolvedSubject, |
| 58 | + from: $from, |
| 59 | + replyTo: [$fromAddress], |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function content(): Content |
| 64 | + { |
| 65 | + return new Content( |
| 66 | + view: 'emails.dynamic-content', |
| 67 | + with: [ |
| 68 | + 'bodyHtml' => $this->renderedBody, |
| 69 | + 'isPlain' => $this->isPlain, |
| 70 | + ], |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + protected function substituteVariables(string $html, array $variables): string |
| 75 | + { |
| 76 | + $urlKeys = ['reset_link', 'app_url', 'link']; |
| 77 | + $variables['app_url'] = $variables['app_url'] ?? config('app.url'); |
| 78 | + |
| 79 | + foreach ($variables as $key => $value) { |
| 80 | + $placeholder = '{{ ' . $key . ' }}'; |
| 81 | + if (in_array($key, $urlKeys, true)) { |
| 82 | + $html = str_replace($placeholder, (string) $value, $html); |
| 83 | + } else { |
| 84 | + $html = str_replace($placeholder, e((string) $value), $html); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return $html; |
| 89 | + } |
| 90 | + |
| 91 | + public function failed(\Throwable $e): void |
| 92 | + { |
| 93 | + EmailLog::create([ |
| 94 | + 'user_id' => null, |
| 95 | + 'recipient_email' => $this->recipientEmail ?: 'unknown', |
| 96 | + 'template_key' => $this->templateKey, |
| 97 | + 'status' => 'failed', |
| 98 | + 'error_details' => $e->getMessage(), |
| 99 | + 'sent_at' => null, |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->notifyAdminOfFailure(); |
| 103 | + } |
| 104 | + |
| 105 | + protected function notifyAdminOfFailure(): void |
| 106 | + { |
| 107 | + try { |
| 108 | + $admins = \App\Models\User::role('admin')->get(); |
| 109 | + $message = sprintf( |
| 110 | + 'E-mail falhou após 3 tentativas: template "%s", destinatário "%s". Verifique os Logs de Mensageria.', |
| 111 | + $this->templateKey, |
| 112 | + $this->recipientEmail |
| 113 | + ); |
| 114 | + foreach ($admins as $admin) { |
| 115 | + $admin->notify(new \Modules\Notifications\Notifications\SystemNotification( |
| 116 | + 'Falha no envio de e-mail', |
| 117 | + $message, |
| 118 | + null, |
| 119 | + 'danger' |
| 120 | + )); |
| 121 | + } |
| 122 | + } catch (\Throwable $e) { |
| 123 | + report($e); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments