Skip to content

Commit

Permalink
mail configurado de outra forma
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Nov 30, 2023
1 parent af43f9a commit da7351a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 44 deletions.
24 changes: 16 additions & 8 deletions exemplos/boleto_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head><body><img src="{{ $logo }}"/><h1>{{ $empresa }}</h1></body></html>',
$data,
],
'assunto'
['<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head><body><img src="{{ $logo }}"/><h1>{{ $empresa }}</h1></body></html>', $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');
103 changes: 67 additions & 36 deletions src/Boleto/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand All @@ -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'=> []]");
Expand All @@ -236,64 +250,81 @@ 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', [
'mime' => 'application/pdf',
])
->from($this->from['address'], $this->from['name'])
->subject($subject)
->to($this->getEmails());
->to($this->getTo());
} else {
$message
->attachData($this->getPdf(), 'boleto.pdf', [
'mime' => 'application/pdf',
])
->setFrom($this->from['address'], $this->from['name'])
->setSubject($subject)
->setTo($this->getEmails());
->setTo($this->getTo());
}
});

Expand Down

0 comments on commit da7351a

Please sign in to comment.