Mailer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\mailer;
  3. use \Exception;
  4. use \MGModule\DNSManager2\mgLibs\custom\mailer\mergefields\Other;
  5. use \MGModule\DNSManager2\models\whmcs\configuration;
  6. use \MGModule\DNSManager2\models\whmcs\emailtemplate;
  7. class Mailer //Działa ale pasuje ładnie porozdzielać
  8. {
  9. private $mail = false;
  10. private $vars = array();
  11. protected $phpMailerNs;
  12. public function __construct()
  13. {
  14. $this->isValidPhpMailer();
  15. $this->mail = new $this->phpMailerNs();
  16. $this->mail->CharSet = "UTF-8";
  17. $this->mail->ContentType = 'text/html';
  18. $this->mail->Encoding = "8bit";
  19. $this->loadWHMCSMailerConfig();
  20. }
  21. protected function isValidPhpMailer()
  22. {
  23. if(class_exists('\\PHPMailer')) {
  24. $this->phpMailerNs = '\\PHPMailer';
  25. return;
  26. }
  27. if(class_exists('\\PHPMailer\\PHPMailer\\PHPMailer')) {
  28. $this->phpMailerNs = '\\PHPMailer\\PHPMailer\\PHPMailer';
  29. return;
  30. }
  31. throw new Exception('There is no PHPMailer class');
  32. }
  33. public function __call($name, $arguments)
  34. {
  35. call_user_func_array(array($this->mail, $name), $arguments);
  36. }
  37. public function loadWHMCSMailerConfig()
  38. {
  39. $mailConfig = configuration\configuration::bySetting('MailConfig')->value;
  40. if(!empty($mailConfig))
  41. {
  42. $configuration = json_decode(decrypt($mailConfig));
  43. $this->mail->Host = $configuration->configuration->host;
  44. $this->mail->Port = $configuration->configuration->port;
  45. $this->mail->Username = $configuration->configuration->username;
  46. $this->mail->Password = $configuration->configuration->password;
  47. $this->mail->SMTPSecure = $configuration->configuration->secure;
  48. }
  49. else
  50. {
  51. $this->mail->Host = configuration\configuration::bySetting('SMTPHost')->value;
  52. $this->mail->Port = configuration\configuration::bySetting('SMTPPort')->value;
  53. $this->mail->Username = configuration\configuration::bySetting('SMTPUsername')->value;
  54. $this->mail->Password = decrypt(configuration\configuration::bySetting('SMTPPassword')->value);
  55. $this->mail->SMTPSecure = configuration\configuration::bySetting('SMTPSSL')->value;
  56. }
  57. $this->mail->mailType = configuration\configuration::bySetting('MailType')->value;
  58. $this->mail->setFrom(configuration\configuration::bySetting('SystemEmailsFromEmail')->value, configuration\configuration::bySetting('SystemEmailsFromName')->value);
  59. }
  60. public function send()
  61. {
  62. if($this->mail->mailType === 'smtp')
  63. {
  64. $this->mail->isSMTP();
  65. $this->mail->SMTPAuth = true;
  66. }
  67. if(!$this->mail->send())
  68. {
  69. \logModuleCall('DNS Manager', 'Mailer Error', $this->mail->ErrorInfo, "", array(), array(), array());
  70. }
  71. }
  72. public function sendWHMCSTemplate($name, $type = 'admin', $relid = false)
  73. {
  74. $template = emailtemplate\repository::factory()->byName($name)->byType($type)->one();
  75. if($template === false) {
  76. return;
  77. }
  78. $this->loadVariables('other');
  79. $this->sendFromSmartyTemplate(html_entity_decode($template->subject), html_entity_decode($template->message));
  80. }
  81. public function sendFromSmartyTemplate($subject, $message, $variables = false)
  82. {
  83. if($variables !== false) {
  84. $this->addVariables($variables);
  85. }
  86. $message = SmartyHelper::fetchFromString($message, $this->vars);
  87. $this->mail->Subject = $subject;
  88. $this->mail->Body = $message;
  89. $this->send();
  90. }
  91. public function loadVariables($type, $relid = false)
  92. {
  93. switch($type) {
  94. case 'other':
  95. $obj = new Other();
  96. break;
  97. }
  98. $this->addVariables($obj->getMergeFields());
  99. }
  100. public function addVariable($name, $value)
  101. {
  102. $this->vars[$name] = $value;
  103. }
  104. public function addVariables($vars)
  105. {
  106. foreach($vars as $k => $v) {
  107. $this->addVariable($k, $v);
  108. }
  109. }
  110. }