File: /var/www/ipsremont-demo/app/Mail/BaseMail.php
<?php
namespace App\Mail;
use App\Services\Email\EmailsService;
use App\Services\Email\EmailTemplatesService;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class BaseMail extends Mailable
{
use Queueable, SerializesModels;
private array $variables;
private EmailsService $emailService;
private EmailTemplatesService $emailTemplateService;
public function __construct($variables)
{
$this->variables = $variables;
$this->emailService = new EmailsService();
$this->emailTemplateService = new EmailTemplatesService();
}
public function build()
{
$header = EmailTemplatesService::getTplForLetter('layout', $this->variables['user']->lang, 'header');
$footer = EmailTemplatesService::getTplForLetter('layout', $this->variables['user']->lang, 'footer');
$body = EmailTemplatesService::getTplForLetter($this->variables['event'], $this->variables['user']->lang,
$this->variables['user']->roles()->get()->pluck('type')->first());
if($this->variables['event'] != 'new-user')
$this->variables['placeholders']['{password}'] = '********';
$header->body = $this->emailTemplateService->replace($header->body, $this->variables['placeholders']);
$footer->body = $this->emailTemplateService->replace($footer->body, $this->variables['placeholders']);
$body->body = $this->emailTemplateService->replace($body->body, $this->variables['placeholders']);
$body->subject = $this->emailTemplateService->replace($body->subject, $this->variables['placeholders']);
$db_subject = $body->subject;
$db_body = $body->body;
if($this->variables['event'] == 'new-user') {
$db_subject = str_replace($this->variables['placeholders']['{password}'],'********', $body->subject);
$db_body = str_replace($this->variables['placeholders']['{password}'],'********', $body->body);
}
$data = [
'subject' => $db_subject,
'body' => $header->body . $db_body . $footer->body,
'event' => __('emails.' . $this->variables['event']),
'recipient' => $this->variables['user']->roles()->get()->pluck('name')->first(),
'user_id' => $this->variables['user']->id,
'name' => $this->variables['user']->name,
'email' => $this->variables['user']->email,
];
$this->emailService->save($data);
return $this
->subject($body->subject)
->markdown('emails.emailTemplateLayout', ['header' => $header->body, 'footer' => $footer->body, 'body' => $body->body]);
}
}