From da7351ad26ee5d1873d5e33b1083226e6f560cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Gusm=C3=A3o?= Date: Thu, 30 Nov 2023 15:07:58 +0000 Subject: [PATCH] mail configurado de outra forma --- exemplos/boleto_mail.php | 24 ++++++--- src/Boleto/Mail.php | 103 +++++++++++++++++++++++++-------------- 2 files changed, 83 insertions(+), 44 deletions(-) diff --git a/exemplos/boleto_mail.php b/exemplos/boleto_mail.php index 608b8ea8..da26899a 100644 --- a/exemplos/boleto_mail.php +++ b/exemplos/boleto_mail.php @@ -49,24 +49,32 @@ 'from' => ['address' => 'empresa@empresa.com', 'name' => 'Empresa'], ]; -$mail = new Eduardokum\LaravelBoleto\Boleto\Mail($boleto, 'email@cliente.com', $configsCasoNaoTenhaUmMailerConfiguradoNoSeuLaravel); +$mail = new Eduardokum\LaravelBoleto\Boleto\Mail($configsCasoNaoTenhaUmMailerConfiguradoNoSeuLaravel); $data = [ 'empresa' => 'Nome da empresa', 'logo' => 'full/path/logo.png', ]; -$mail->send(['arquivos/template.blade.php', $data], 'assunto'); +$mail->send( + ['arquivos/template.blade.php', $data], + 'assunto', + $boleto, + 'email@cliente.com' +); $mail->send( - [ - '

{{ $empresa }}

', - $data, - ], - 'assunto' + ['

{{ $empresa }}

', $data], + 'assunto', + $boleto, + 'email@cliente.com' ); $mail->send( 'Email simples sem template', - 'assunto' + 'assunto', + $boleto, + 'email@cliente.com' ); + +$mail->setTo('email@cliente.com')->setBoleto($boleto)->send('Email simples sem template', 'assunto'); diff --git a/src/Boleto/Mail.php b/src/Boleto/Mail.php index 1e9a6e63..675673e4 100644 --- a/src/Boleto/Mail.php +++ b/src/Boleto/Mail.php @@ -7,11 +7,11 @@ use Swift_SmtpTransport; use Illuminate\Support\Arr; use Illuminate\Mail\Message; -use Illuminate\View\Factory; use Illuminate\Config\Repository; use Eduardokum\LaravelBoleto\Blade; use Illuminate\Container\Container; use Symfony\Component\Mailer\Transport\Dsn; +use Illuminate\View\Compilers\BladeCompiler; use Eduardokum\LaravelBoleto\Boleto\Render\Pdf; use Eduardokum\LaravelBoleto\Contracts\Boleto\Boleto; use Illuminate\Contracts\Mail\Factory as MailFactory; @@ -23,28 +23,42 @@ class Mail { + /** + * @var Boleto + */ private $boleto; + /** + * @var array + */ private $from = []; - private $emails = []; + /** + * @var array + */ + private $to = []; + /** + * @var ViewFactory + */ private $view; + /** + * @var LaravelBoletoMailer + */ private $mailer; + /** + * @var BladeCompiler + */ private $blade; /** - * @param Boleto $boleto - * @param $emails * @param array $mailerConfigs * @throws ValidationException */ - public function __construct(Boleto $boleto, $emails, $mailerConfigs = []) + public function __construct($mailerConfigs = []) { - $this->setBoleto($boleto); - $this->setEmails($emails); $this->makeBlade(); $this->makeMailer($mailerConfigs); } @@ -72,13 +86,13 @@ private function getBoleto() /** * @return array */ - private function getEmails() + private function getTo() { - return $this->emails; + return $this->to; } /** - * @return Factory + * @return BladeCompiler */ private function getBlade() { @@ -175,7 +189,7 @@ private function makeMailer($config) $config['host'], $config['username'] ?? null, $config['password'] ?? null, - $config['port'] ?? null, + ((int) $config['port']) ?? null, $config )); $this->mailer = new LaravelBoletoMailer('default', $this->view, $transport); @@ -199,19 +213,19 @@ private function makeMailer($config) } /** - * @param $texto + * @param $template * @return string|void|null * @throws ValidationException */ - private function build($texto) + private function build($template) { - if (is_string($texto)) { - return $texto; + if (is_string($template)) { + return $template; } - if (is_array($texto)) { - $view = Arr::get($texto, 'view', Arr::get($texto, 'template', Arr::get($texto, 0))); - $data = Arr::get($texto, 'data', Arr::get($texto, 'vars', Arr::get($texto, 1, []))); + if (is_array($template)) { + $view = Arr::get($template, 'view', Arr::get($template, 'template', Arr::get($template, 0))); + $data = Arr::get($template, 'data', Arr::get($template, 'vars', Arr::get($template, 1, []))); if (is_null($view)) { throw new ValidationException("View não informada, Utilizar ['view' => 'template.blade.php', 'data'=> []]"); @@ -236,48 +250,65 @@ private function build($texto) } /** - * @param mixed $boleto + * @param array|string $to * @return Mail */ - public function setBoleto($boleto) + public function setTo($to) { - $this->boleto = $boleto; + $this->to = is_array($to) ? $to : [$to]; + + foreach ($this->to as $i => $email) { + if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { + unset($this->to[$i]); + } + } return $this; } /** - * @param array $emails + * @param Boleto $boleto * @return Mail */ - public function setEmails($emails) + public function setBoleto(Boleto $boleto) { - $this->emails = is_array($emails) ? $emails : [$emails]; - - foreach ($this->emails as $i => $email) { - if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { - unset($this->emails[$i]); - } - } + $this->boleto = $boleto; return $this; } /** - * @param $texto + * @param $template * @param $subject + * @param Boleto|null $boleto + * @param null $to * @return bool + * @throws ValidationException */ - public function send($texto, $subject) + public function send($template, $subject, Boleto $boleto = null, $to = null) { + if ($to) { + $this->setTo($to); + } + if ($boleto) { + $this->setBoleto($boleto); + } + + if (! $this->getBoleto()) { + throw new ValidationException('Informe o boleto a ser enviado utilizando o método ->setBoleto ou passando #3 parâmetro no método ->send'); + } + if (! $this->getTo()) { + throw new ValidationException('Informe o destinatário utilizando o método ->setTo ou passando #4 parâmetro no método ->send'); + } + try { - $texto = $this->build($texto); + $html = $this->build($template); if (! LaravelBoletoMailer::isLaravel9Plus() && ! app()->bound(EmbedImages::class)) { $this->getMailer()->getSwiftMailer()->registerPlugin(new SwiftEmbedImages(config()->get('mail-auto-embed'))); } - $this->getMailer()->html($texto, function (Message $message) use ($subject) { + $this->getMailer()->html($html, function (Message $message) use ($subject) { if (LaravelBoletoMailer::isLaravel9Plus()) { $message ->attachData($this->getPdf(), 'boleto.pdf', [ @@ -285,7 +316,7 @@ public function send($texto, $subject) ]) ->from($this->from['address'], $this->from['name']) ->subject($subject) - ->to($this->getEmails()); + ->to($this->getTo()); } else { $message ->attachData($this->getPdf(), 'boleto.pdf', [ @@ -293,7 +324,7 @@ public function send($texto, $subject) ]) ->setFrom($this->from['address'], $this->from['name']) ->setSubject($subject) - ->setTo($this->getEmails()); + ->setTo($this->getTo()); } });