EmailService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. use ModulesGarden\ProxmoxAddon\Core\Api\Whmcs;
  4. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Admins;
  5. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\EmailTemplate;
  6. class EmailService
  7. {
  8. private $api;
  9. private $templateId;
  10. private $vars = [];
  11. private $lastResponse;
  12. /**
  13. * EmailService constructor.
  14. * @param $api
  15. */
  16. public function __construct()
  17. {
  18. $this->api = new Whmcs(new Admins());
  19. }
  20. public function template($templateId)
  21. {
  22. if (!$templateId)
  23. {
  24. throw new \InvalidArgumentException("Email Template Id cannot be empty");
  25. }
  26. $this->templateId = $templateId;
  27. $this->vars = EmailTemplate::select("name", "type")->where("id", $this->templateId)->first()->toArray();
  28. $this->vars['messagename'] = $this->vars['name'];
  29. unset($this->vars['name']);
  30. return $this;
  31. }
  32. public function vars(array $vars)
  33. {
  34. $this->vars = array_merge($this->vars, $vars);
  35. return $this;
  36. }
  37. public function getVars()
  38. {
  39. return $this->vars;
  40. }
  41. public function send()
  42. {
  43. $this->lastResponse = $this->api->call("sendemail", $this->vars);
  44. $this->vars = [];
  45. $this->templateId = null;
  46. return $this;
  47. }
  48. public function sendToAdmin()
  49. {
  50. if (function_exists('sendAdminMessage'))
  51. {
  52. /**
  53. * sendAdminMessage ($templateName, $adminId = "", $templateVars = array(), $to = "system", $deptid = "", $ticketNotify = "")
  54. */
  55. sendAdminMessage($this->vars['messagename'], $this->vars);
  56. $this->vars = [];
  57. $this->templateId = null;
  58. }
  59. return $this;
  60. }
  61. /**
  62. * @return mixed
  63. */
  64. public function getLastResponse()
  65. {
  66. return $this->lastResponse;
  67. }
  68. }