<?php
/**
* Created by PhpStorm.
* User: Ronald
* Date: 4/8/2020
* Time: 12:00 PM
*/
namespace App\Mailer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Twig\Environment;
/**
* Class Mailer
* @package App\Mailer
* @property MailerInterface $mailer
* @property Environment $twig
*/
abstract class Mailer
{
protected $mailer, $twig, $env,$container;
protected $mainEmail;
/**
* Mailer constructor.
* @param MailerInterface $mailer
* @param ContainerInterface $container
* @throws \Exception
*/
public function __construct(MailerInterface $mailer, ContainerInterface $container,$env)
{
$this->mailer = $mailer;
$this->env = $env;
$this->container = $container;
$this->mainEmail ='backoffice@overseasnetwork.com';
//Check case for GTN Xpress. This will work when this class is called from GTN domain.
if($this->container->get('session')->get('customerAccountDomain')){
$this->mainEmail ='support@gtnxpress.com';
}
if($env != 'prod'){
$this->mainEmail = 'oxtest@overseasinternational.com';
}
$this->twig = $container->get('twig');
}
/**
* In some cases, such as when calling this class from the NotificationSystemMailer class,
* it is necessary to change the $mainEmail
* @param $mainEmail
* @return void
*/
public function setMainEmail($mainEmail)
{
if($this->env == 'prod'){
$this->mainEmail = $mainEmail;
}
}
protected function send($subject, $body, $to = array(), $attached = null, $cc = array(),$sendDefaultCC = true,$fromEmail=null)
{
$email = new Email();
$email->from($fromEmail?$fromEmail:$this->mainEmail);
if ($this->env == 'prod') {
if (!is_array($to)) {
$to = array($to);
}elseif(!count($to)){
$to = array($this->mainEmail);
}
$email->to(...$to);
if (!is_array($cc)) {
$cc = array($cc);
}elseif($sendDefaultCC && !count($cc)){
$cc = array($this->mainEmail);
}
if ($to !== $cc) {
$email->cc(...$cc);
}
} else {
$email->to($this->mainEmail);
}
// print_r(array_merge($to,$cc));
$email->subject($subject)->html($body);
if ($attached) {
foreach ($attached as $at){
$email->attach($at['file'],$at['name'],$at['type']);
}
}
$this->mailer->send($email);
}
}