| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\mailer;
- use \Exception;
- use \MGModule\DNSManager2\mgLibs\custom\mailer\mergefields\Other;
- use \MGModule\DNSManager2\models\whmcs\configuration;
- use \MGModule\DNSManager2\models\whmcs\emailtemplate;
- class Mailer //Działa ale pasuje ładnie porozdzielać
- {
- private $mail = false;
- private $vars = array();
- protected $phpMailerNs;
- public function __construct()
- {
- $this->isValidPhpMailer();
- $this->mail = new $this->phpMailerNs();
- $this->mail->CharSet = "UTF-8";
- $this->mail->ContentType = 'text/html';
- $this->mail->Encoding = "8bit";
- $this->loadWHMCSMailerConfig();
- }
- protected function isValidPhpMailer()
- {
- if(class_exists('\\PHPMailer')) {
- $this->phpMailerNs = '\\PHPMailer';
- return;
- }
- if(class_exists('\\PHPMailer\\PHPMailer\\PHPMailer')) {
- $this->phpMailerNs = '\\PHPMailer\\PHPMailer\\PHPMailer';
- return;
- }
- throw new Exception('There is no PHPMailer class');
- }
- public function __call($name, $arguments)
- {
- call_user_func_array(array($this->mail, $name), $arguments);
- }
- public function loadWHMCSMailerConfig()
- {
- $mailConfig = configuration\configuration::bySetting('MailConfig')->value;
-
- if(!empty($mailConfig))
- {
- $configuration = json_decode(decrypt($mailConfig));
- $this->mail->Host = $configuration->configuration->host;
- $this->mail->Port = $configuration->configuration->port;
- $this->mail->Username = $configuration->configuration->username;
- $this->mail->Password = $configuration->configuration->password;
- $this->mail->SMTPSecure = $configuration->configuration->secure;
- }
- else
- {
- $this->mail->Host = configuration\configuration::bySetting('SMTPHost')->value;
- $this->mail->Port = configuration\configuration::bySetting('SMTPPort')->value;
- $this->mail->Username = configuration\configuration::bySetting('SMTPUsername')->value;
- $this->mail->Password = decrypt(configuration\configuration::bySetting('SMTPPassword')->value);
- $this->mail->SMTPSecure = configuration\configuration::bySetting('SMTPSSL')->value;
- }
-
- $this->mail->mailType = configuration\configuration::bySetting('MailType')->value;
- $this->mail->setFrom(configuration\configuration::bySetting('SystemEmailsFromEmail')->value, configuration\configuration::bySetting('SystemEmailsFromName')->value);
- }
- public function send()
- {
- if($this->mail->mailType === 'smtp')
- {
- $this->mail->isSMTP();
- $this->mail->SMTPAuth = true;
- }
-
- if(!$this->mail->send())
- {
- \logModuleCall('DNS Manager', 'Mailer Error', $this->mail->ErrorInfo, "", array(), array(), array());
- }
- }
- public function sendWHMCSTemplate($name, $type = 'admin', $relid = false)
- {
- $template = emailtemplate\repository::factory()->byName($name)->byType($type)->one();
- if($template === false) {
- return;
- }
- $this->loadVariables('other');
- $this->sendFromSmartyTemplate(html_entity_decode($template->subject), html_entity_decode($template->message));
- }
- public function sendFromSmartyTemplate($subject, $message, $variables = false)
- {
- if($variables !== false) {
- $this->addVariables($variables);
- }
- $message = SmartyHelper::fetchFromString($message, $this->vars);
- $this->mail->Subject = $subject;
- $this->mail->Body = $message;
- $this->send();
- }
- public function loadVariables($type, $relid = false)
- {
- switch($type) {
- case 'other':
- $obj = new Other();
- break;
- }
- $this->addVariables($obj->getMergeFields());
- }
- public function addVariable($name, $value)
- {
- $this->vars[$name] = $value;
- }
- public function addVariables($vars)
- {
- foreach($vars as $k => $v) {
- $this->addVariable($k, $v);
- }
- }
- }
|