src/Services/EmailService.php line 115

Open in your IDE?
  1. <?php 
  2. namespace EADPlataforma\Services;
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use EADPlataforma\Services\GeneralService;
  5. use EADPlataforma\Services\FileService;
  6. use EADPlataforma\Entity\User;
  7. use EADPlataforma\Enum\UserEnum;
  8. use EADPlataforma\Enum\ServicesEnum;
  9.  
  10. class EmailService
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     protected $templateBody;
  16.     /**
  17.      * @var array
  18.      */
  19.     protected $data;
  20.     /**
  21.      * @var string
  22.      */
  23.     protected $fromEmail;
  24.     /**
  25.      * @var string
  26.      */
  27.     protected $fromName;
  28.     /**
  29.      * @var string
  30.      */
  31.     protected $toEmail;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $toName;
  36.     /**
  37.      * @var string
  38.      */
  39.     protected $replyEmail;
  40.     /**
  41.      * @var string
  42.      */
  43.     protected $replyName;
  44.     /**
  45.      * @var string
  46.      */
  47.     protected $subject;
  48.     /**
  49.      * @var array
  50.      */
  51.     protected $attachments;
  52.     /**
  53.      * @var Symfony\Component\DependencyInjection\ContainerInterface
  54.      */
  55.     protected $container;
  56.     /**
  57.      * @var GeneralService
  58.      */
  59.     protected $generalService;
  60.     /**
  61.      * @var FileService
  62.      */
  63.     protected $fileService;
  64.     /**
  65.      * @var ConfigurationService
  66.      */
  67.     protected $configuration;
  68.     /**
  69.      * @var StringUtil
  70.      */
  71.     protected $stringUtil;
  72.     /**
  73.      * @var SchoolEntityManager
  74.      */
  75.     protected $em;
  76.     /**
  77.      * @var \Client
  78.      */
  79.     protected $client;
  80.     /**
  81.      * @var \Client
  82.      */
  83.     protected $info;
  84.     /**
  85.      * @var \Templating
  86.      */
  87.     protected $engineTemplate;
  88.     /**
  89.      * Constructor
  90.      *
  91.      * @param GeneralService $generalService
  92.      */
  93.     public function __construct(GeneralService $generalService)
  94.     {
  95.         $this->generalService $generalService;
  96.         $this->container $this->generalService->getContainer();
  97.         $this->configuration $this->generalService->getService('ConfigurationService');
  98.         $this->fileService $this->generalService->getService('FileService');
  99.         $this->stringUtil $this->generalService->getUtil('StringUtil');
  100.         $this->engineTemplate $this->container->get('twig');
  101.         $this->em $this->generalService->getService('SchoolEntityManager');
  102.         $this->info $generalService->getServiceAccess(ServicesEnum::AWS_SES);
  103.         $this->attachments = [];
  104.         $this->client $this->configuration->getClient();
  105.         $this->fromEmail $this->info->emailFrom;
  106.         $this->fromName $this->client->getBrand();
  107.         $this->replyEmail $this->client->getEmail();
  108.         $this->replyName $this->client->getBrand();
  109.     }
  110.     public function setTemplateBody(?string $templateBody): self
  111.     {
  112.         $this->templateBody $templateBody;
  113.         return $this;
  114.     }
  115.     public function getTemplateBody(): ?string
  116.     {
  117.         return $this->templateBody;
  118.     }
  119.     public function setData(?array $data): self
  120.     {
  121.         $this->data $data;
  122.         return $this;
  123.     }
  124.     public function getData(): ?array
  125.     {
  126.         return $this->data;
  127.     }
  128.     public function setFromEmail(?string $fromEmail): self
  129.     {
  130.         $this->fromEmail $fromEmail;
  131.         return $this;
  132.     }
  133.     public function getFromEmail(): ?string
  134.     {
  135.         return $this->fromEmail;
  136.     }
  137.     public function setFromName(?string $fromName): self
  138.     {
  139.         $this->fromName $fromName;
  140.         return $this;
  141.     }
  142.     public function getFromName(): ?string
  143.     {
  144.         return $this->fromName;
  145.     }
  146.     public function setToEmail(?string $toEmail): self
  147.     {
  148.         $this->toEmail $toEmail;
  149.         return $this;
  150.     }
  151.     public function getToEmail(): ?string
  152.     {
  153.         return $this->toEmail;
  154.     }
  155.     public function setToName(?string $toName): self
  156.     {
  157.         $this->toName $toName;
  158.         return $this;
  159.     }
  160.     public function getToName(): ?string
  161.     {
  162.         return $this->toName;
  163.     }
  164.     public function setReplyEmail(?string $replyEmail): self
  165.     {
  166.         $this->replyEmail $replyEmail;
  167.         return $this;
  168.     }
  169.     public function getReplyEmail(): ?string
  170.     {
  171.         return $this->replyEmail;
  172.     }
  173.     public function setReplyName(?string $replyName): self
  174.     {
  175.         $this->replyName $replyName;
  176.         return $this;
  177.     }
  178.     public function getReplyName(): ?string
  179.     {
  180.         return $this->replyName;
  181.     }
  182.     public function setSubject(?string $subject): self
  183.     {
  184.         $this->subject $subject;
  185.         return $this;
  186.     }
  187.     public function getSubject(): ?string
  188.     {
  189.         return $this->subject;
  190.     }
  191.     public function setAttachments(array $attachments): self
  192.     {
  193.         $this->attachments $attachments;
  194.         return $this;
  195.     }
  196.     public function getAttachments(): array
  197.     {
  198.         return $this->attachments;
  199.     }
  200.     public function checkUserToSend(User $user$checkStatus true)
  201.     {
  202.         if($checkStatus){
  203.             $status $user->getStatus();
  204.             if($status == UserEnum::BLOCK){
  205.                 return false;
  206.             }
  207.         }
  208.         $email $user->getEmail();
  209.         if($user->getValidEmail() == UserEnum::UNKNOWN){
  210.             $user->setValidEmail($this->stringUtil->validEmail($email));
  211.             $this->em->flush();
  212.         }
  213.         return ($user->getValidEmail() == UserEnum::DELIVERABLE);
  214.     }
  215.     public function checkEmailToSend(string $email)
  216.     {
  217.         return ($this->stringUtil->validEmail($email) == UserEnum::DELIVERABLE);
  218.     }
  219.     protected function prepareEmailContent(): string
  220.     {
  221.         $logo $this->configuration->get("logo");
  222.         $logoEmail $this->configuration->get("logo_email");
  223.         $clientDomain "https:{$this->configuration->getActiveDomain(true)}";
  224.         $client $this->configuration->getClient();
  225.         $eadDomain $client->getDomainPrimary();
  226.         $logo = (!empty($logoEmail) ? $logoEmail $logo);
  227.         $logoSrc "https://{$eadDomain}/upload/others/{$logo}?option=logo-email";
  228.         $clientData = [
  229.             "templateContent" => "",
  230.             "title" => $this->getSubject(),
  231.             "clientId" => $this->client->getClientId(),
  232.             "clientDomain" => $clientDomain,
  233.             "clientBrand" => $this->client->getBrand(),
  234.             "clientLogo" => $logoSrc,
  235.             "clientPrimaryColor" => $this->configuration->get("primary_color"),
  236.             "clientSecondColor" => $this->configuration->get("second_color"),
  237.             "today" => date("Y-m-d"),
  238.             "year" => date("Y"),
  239.         ];
  240.         
  241.         $data array_merge($clientData$this->getData());
  242.         foreach ($data as $key => $value) {
  243.             if(is_string($value)){
  244.                 if(stristr($value'="/upload')){
  245.                     $value str_replace('="/upload''="https://' $eadDomain '/upload'$value);
  246.                 }
  247.             }
  248.             $data[$key] = $value;
  249.         }
  250.         $templateContent $this->engineTemplate->render(
  251.             "email/{$this->getTemplateBody()}.html.twig"
  252.             $data
  253.         );
  254.         $data["templateContent"] = $templateContent;
  255.         $template $this->engineTemplate->render("email/email_template.html.twig"$data);
  256.         
  257.         return $template;
  258.     }
  259.     public function send(){
  260.         // Instanciamos a classe
  261.         $email = new PHPMailer();
  262.         
  263.         // Informamos que a classe ira enviar o email por SMTP
  264.         $email->isSMTP();
  265.         // $email->SMTPDebug  = 2;
  266.         
  267.         // Remetente da mensagem
  268.         $email->Host $this->info->host;
  269.         $email->SMTPSecure "ssl";
  270.         $email->SMTPAuth true;
  271.         $email->SMTPDebug false;
  272.         $email->Port     465;
  273.         $email->Username $this->info->key;
  274.         $email->Password $this->info->secret;
  275.         // Iremos enviar o email no formato HTML
  276.         $email->IsHTML(true);
  277.         $email->CharSet 'utf-8';
  278.         $typeEmail null;
  279.         if(isset(ServicesEnum::EMAILS[$this->getTemplateBody()])){
  280.             $typeEmail ServicesEnum::EMAILS[$this->getTemplateBody()];
  281.             $email->addCustomHeader('type'ServicesEnum::EMAILS[$this->getTemplateBody()]);
  282.         }
  283.         $dataHead = [
  284.             "origin" => (string)ServicesEnum::ORIGIN_PLATFORM,
  285.             "type" => (string)$typeEmail,
  286.             "client_id" => (string)$this->client->getClientId(),
  287.         ];
  288.         $email->addCustomHeader('X-MT-Custom-Variables'json_encode($dataHead));
  289.         // CabeƧalhos personalizados com variĆ”veis customizadas
  290.         $email->addCustomHeader('origin'ServicesEnum::ORIGIN_PLATFORM);
  291.         
  292.         $email->addCustomHeader('client_id'$this->client->getClientId());
  293.         $email->addReplyTo($this->replyEmail$this->replyName);
  294.         $email->From $this->fromEmail;
  295.         $email->FromName $this->stringUtil->cleanString2($this->fromName);
  296.         $content $this->prepareEmailContent();
  297.         $email->Subject $this->subject;
  298.         $email->AddAddress($this->toEmail$this->toName);
  299.         $email->Body $content;
  300.         if(count($this->getAttachments()) > 0){
  301.             foreach ($this->getAttachments() as $name => $attachment) {
  302.                 $email->addAttachment($attachment$name);
  303.             }
  304.         }
  305.         try{
  306.             $email->Send();
  307.             $email->ClearAllRecipients();
  308.         } catch (\Exception $e) {
  309.             print_r($e->getMessage());
  310.             exit;
  311.         }
  312.         return true;
  313.     }
  314. }