<?php
namespace EADPlataforma\Services;
use PHPMailer\PHPMailer\PHPMailer;
use EADPlataforma\Services\GeneralService;
use EADPlataforma\Services\FileService;
use EADPlataforma\Entity\User;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\ServicesEnum;
class EmailService
{
/**
* @var string
*/
protected $templateBody;
/**
* @var array
*/
protected $data;
/**
* @var string
*/
protected $fromEmail;
/**
* @var string
*/
protected $fromName;
/**
* @var string
*/
protected $toEmail;
/**
* @var string
*/
protected $toName;
/**
* @var string
*/
protected $replyEmail;
/**
* @var string
*/
protected $replyName;
/**
* @var string
*/
protected $subject;
/**
* @var array
*/
protected $attachments;
/**
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* @var GeneralService
*/
protected $generalService;
/**
* @var FileService
*/
protected $fileService;
/**
* @var ConfigurationService
*/
protected $configuration;
/**
* @var StringUtil
*/
protected $stringUtil;
/**
* @var SchoolEntityManager
*/
protected $em;
/**
* @var \Client
*/
protected $client;
/**
* @var \Client
*/
protected $info;
/**
* @var \Templating
*/
protected $engineTemplate;
/**
* Constructor
*
* @param GeneralService $generalService
*/
public function __construct(GeneralService $generalService)
{
$this->generalService = $generalService;
$this->container = $this->generalService->getContainer();
$this->configuration = $this->generalService->getService('ConfigurationService');
$this->fileService = $this->generalService->getService('FileService');
$this->stringUtil = $this->generalService->getUtil('StringUtil');
$this->engineTemplate = $this->container->get('twig');
$this->em = $this->generalService->getService('SchoolEntityManager');
$this->info = $generalService->getServiceAccess(ServicesEnum::AWS_SES);
$this->attachments = [];
$this->client = $this->configuration->getClient();
$this->fromEmail = $this->info->emailFrom;
$this->fromName = $this->client->getBrand();
$this->replyEmail = $this->client->getEmail();
$this->replyName = $this->client->getBrand();
}
public function setTemplateBody(?string $templateBody): self
{
$this->templateBody = $templateBody;
return $this;
}
public function getTemplateBody(): ?string
{
return $this->templateBody;
}
public function setData(?array $data): self
{
$this->data = $data;
return $this;
}
public function getData(): ?array
{
return $this->data;
}
public function setFromEmail(?string $fromEmail): self
{
$this->fromEmail = $fromEmail;
return $this;
}
public function getFromEmail(): ?string
{
return $this->fromEmail;
}
public function setFromName(?string $fromName): self
{
$this->fromName = $fromName;
return $this;
}
public function getFromName(): ?string
{
return $this->fromName;
}
public function setToEmail(?string $toEmail): self
{
$this->toEmail = $toEmail;
return $this;
}
public function getToEmail(): ?string
{
return $this->toEmail;
}
public function setToName(?string $toName): self
{
$this->toName = $toName;
return $this;
}
public function getToName(): ?string
{
return $this->toName;
}
public function setReplyEmail(?string $replyEmail): self
{
$this->replyEmail = $replyEmail;
return $this;
}
public function getReplyEmail(): ?string
{
return $this->replyEmail;
}
public function setReplyName(?string $replyName): self
{
$this->replyName = $replyName;
return $this;
}
public function getReplyName(): ?string
{
return $this->replyName;
}
public function setSubject(?string $subject): self
{
$this->subject = $subject;
return $this;
}
public function getSubject(): ?string
{
return $this->subject;
}
public function setAttachments(array $attachments): self
{
$this->attachments = $attachments;
return $this;
}
public function getAttachments(): array
{
return $this->attachments;
}
public function checkUserToSend(User $user, $checkStatus = true)
{
if($checkStatus){
$status = $user->getStatus();
if($status == UserEnum::BLOCK){
return false;
}
}
$email = $user->getEmail();
if($user->getValidEmail() == UserEnum::UNKNOWN){
$user->setValidEmail($this->stringUtil->validEmail($email));
$this->em->flush();
}
return ($user->getValidEmail() == UserEnum::DELIVERABLE);
}
public function checkEmailToSend(string $email)
{
return ($this->stringUtil->validEmail($email) == UserEnum::DELIVERABLE);
}
protected function prepareEmailContent(): string
{
$logo = $this->configuration->get("logo");
$logoEmail = $this->configuration->get("logo_email");
$clientDomain = "https:{$this->configuration->getActiveDomain(true)}";
$client = $this->configuration->getClient();
$eadDomain = $client->getDomainPrimary();
$logo = (!empty($logoEmail) ? $logoEmail : $logo);
$logoSrc = "https://{$eadDomain}/upload/others/{$logo}?option=logo-email";
$clientData = [
"templateContent" => "",
"title" => $this->getSubject(),
"clientId" => $this->client->getClientId(),
"clientDomain" => $clientDomain,
"clientBrand" => $this->client->getBrand(),
"clientLogo" => $logoSrc,
"clientPrimaryColor" => $this->configuration->get("primary_color"),
"clientSecondColor" => $this->configuration->get("second_color"),
"today" => date("Y-m-d"),
"year" => date("Y"),
];
$data = array_merge($clientData, $this->getData());
foreach ($data as $key => $value) {
if(is_string($value)){
if(stristr($value, '="/upload')){
$value = str_replace('="/upload', '="https://' . $eadDomain . '/upload', $value);
}
}
$data[$key] = $value;
}
$templateContent = $this->engineTemplate->render(
"email/{$this->getTemplateBody()}.html.twig",
$data
);
$data["templateContent"] = $templateContent;
$template = $this->engineTemplate->render("email/email_template.html.twig", $data);
return $template;
}
public function send(){
// Instanciamos a classe
$email = new PHPMailer();
// Informamos que a classe ira enviar o email por SMTP
$email->isSMTP();
// $email->SMTPDebug = 2;
// Remetente da mensagem
$email->Host = $this->info->host;
$email->SMTPSecure = "ssl";
$email->SMTPAuth = true;
$email->SMTPDebug = false;
$email->Port = 465;
$email->Username = $this->info->key;
$email->Password = $this->info->secret;
// Iremos enviar o email no formato HTML
$email->IsHTML(true);
$email->CharSet = 'utf-8';
$typeEmail = null;
if(isset(ServicesEnum::EMAILS[$this->getTemplateBody()])){
$typeEmail = ServicesEnum::EMAILS[$this->getTemplateBody()];
$email->addCustomHeader('type', ServicesEnum::EMAILS[$this->getTemplateBody()]);
}
$dataHead = [
"origin" => (string)ServicesEnum::ORIGIN_PLATFORM,
"type" => (string)$typeEmail,
"client_id" => (string)$this->client->getClientId(),
];
$email->addCustomHeader('X-MT-Custom-Variables', json_encode($dataHead));
// CabeƧalhos personalizados com variƔveis customizadas
$email->addCustomHeader('origin', ServicesEnum::ORIGIN_PLATFORM);
$email->addCustomHeader('client_id', $this->client->getClientId());
$email->addReplyTo($this->replyEmail, $this->replyName);
$email->From = $this->fromEmail;
$email->FromName = $this->stringUtil->cleanString2($this->fromName);
$content = $this->prepareEmailContent();
$email->Subject = $this->subject;
$email->AddAddress($this->toEmail, $this->toName);
$email->Body = $content;
if(count($this->getAttachments()) > 0){
foreach ($this->getAttachments() as $name => $attachment) {
$email->addAttachment($attachment, $name);
}
}
try{
$email->Send();
$email->ClearAllRecipients();
} catch (\Exception $e) {
print_r($e->getMessage());
exit;
}
return true;
}
}