src/Mailer/Mailer.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Ronald
  5.  * Date: 4/8/2020
  6.  * Time: 12:00 PM
  7.  */
  8. namespace App\Mailer;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Email;
  12. use Twig\Environment;
  13. /**
  14.  * Class Mailer
  15.  * @package App\Mailer
  16.  * @property MailerInterface $mailer
  17.  * @property Environment $twig
  18.  */
  19. abstract class Mailer
  20. {
  21.     protected $mailer$twig$env,$container;
  22.     protected  $mainEmail;
  23.     /**
  24.      * Mailer constructor.
  25.      * @param MailerInterface $mailer
  26.      * @param ContainerInterface $container
  27.      * @throws \Exception
  28.      */
  29.     public function __construct(MailerInterface $mailerContainerInterface $container,$env)
  30.     {
  31.         $this->mailer $mailer;
  32.         $this->env $env;
  33.         $this->container $container;
  34.         $this->mainEmail ='backoffice@overseasnetwork.com';
  35.         //Check case for GTN Xpress. This will work when this class is called from GTN domain.
  36.         if($this->container->get('session')->get('customerAccountDomain')){
  37.             $this->mainEmail ='support@gtnxpress.com';
  38.         }
  39.         if($env != 'prod'){
  40.             $this->mainEmail 'oxtest@overseasinternational.com';
  41.         }
  42.         $this->twig $container->get('twig');
  43.     }
  44.     /**
  45.      * In some cases, such as when calling this class from the NotificationSystemMailer class,
  46.      * it is necessary to change the $mainEmail
  47.      * @param $mainEmail
  48.      * @return void
  49.      */
  50.     public function setMainEmail($mainEmail)
  51.     {
  52.         if($this->env  == 'prod'){
  53.             $this->mainEmail $mainEmail;
  54.         }
  55.     }
  56.     protected function send($subject$body$to = array(), $attached null$cc = array(),$sendDefaultCC true,$fromEmail=null)
  57.     {
  58.         $email = new Email();
  59.         $email->from($fromEmail?$fromEmail:$this->mainEmail);
  60.         if ($this->env == 'prod') {
  61.             if (!is_array($to)) {
  62.                 $to = array($to);
  63.             }elseif(!count($to)){
  64.                 $to = array($this->mainEmail);
  65.             }
  66.             $email->to(...$to);
  67.             if (!is_array($cc)) {
  68.                 $cc = array($cc);
  69.             }elseif($sendDefaultCC && !count($cc)){
  70.                 $cc = array($this->mainEmail);
  71.             }
  72.             if ($to !== $cc) {
  73.                 $email->cc(...$cc);
  74.             }
  75.         } else {
  76.             $email->to($this->mainEmail);
  77.         }
  78.         // print_r(array_merge($to,$cc));
  79.         $email->subject($subject)->html($body);
  80.         if ($attached) {
  81.             foreach ($attached as $at){
  82.                 $email->attach($at['file'],$at['name'],$at['type']);
  83.             }
  84.         }
  85.         $this->mailer->send($email);
  86.     }
  87. }