api.ubillingmail.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Emails sending basic class
  4. */
  5. class UbillingMail {
  6. /**
  7. * Contains telegram messages path
  8. */
  9. const QUEUE_PATH = 'content/mailqueue/';
  10. /**
  11. * Creates new Email queue class instance
  12. */
  13. public function __construct() {
  14. }
  15. /**
  16. * Stores message in email sending queue. Use this method in your modules.
  17. *
  18. * @param string $email
  19. * @param string $subj
  20. * @param string $message
  21. * @param string $module
  22. *
  23. * @return bool
  24. */
  25. public function sendEmail($email, $subj, $message, $module = '') {
  26. $result = false;
  27. $email = trim($email);
  28. $subj = trim($subj);
  29. $module = (!empty($module)) ? ' MODULE ' . $module : '';
  30. if (!empty($email)) {
  31. $message = trim($message);
  32. $filename = self::QUEUE_PATH . 'eml_' . zb_rand_string(8);
  33. $storedata['email'] = $email;
  34. $storedata['subj'] = $subj;
  35. $storedata['message'] = $message;
  36. $storedata = json_encode($storedata);
  37. file_put_contents($filename, $storedata);
  38. log_register('UEML SEND EMAIL `' . $email . '`' . $module);
  39. $result = true;
  40. }
  41. return ($result);
  42. }
  43. /**
  44. * Returns count of emails available in queue
  45. *
  46. * @return int
  47. */
  48. public function getQueueCount() {
  49. $messagesQueueCount = rcms_scandir(self::QUEUE_PATH);
  50. $result = sizeof($messagesQueueCount);
  51. return ($result);
  52. }
  53. /**
  54. * Returns array containing all emails queue data as index=>data
  55. *
  56. * @return array
  57. */
  58. public function getQueueData() {
  59. $result = array();
  60. $messagesQueue = rcms_scandir(self::QUEUE_PATH);
  61. if (!empty($messagesQueue)) {
  62. foreach ($messagesQueue as $io => $eachmessage) {
  63. $messageDate = date("Y-m-d H:i:s", filectime(self::QUEUE_PATH . $eachmessage));
  64. $messageData = file_get_contents(self::QUEUE_PATH . $eachmessage);
  65. $messageData = json_decode($messageData, true);
  66. $result[$io]['filename'] = $eachmessage;
  67. $result[$io]['date'] = $messageDate;
  68. $result[$io]['email'] = $messageData['email'];
  69. $result[$io]['subj'] = $messageData['subj'];
  70. $result[$io]['message'] = $messageData['message'];
  71. }
  72. }
  73. return ($result);
  74. }
  75. /**
  76. * Deletes message from local queue
  77. *
  78. * @param string $filename Existing message filename
  79. *
  80. * @return int 0 - ok, 1 - deletion unsuccessful, 2 - file not found
  81. */
  82. public function deleteEmail($filename) {
  83. if (file_exists(self::QUEUE_PATH . $filename)) {
  84. rcms_delete_files(self::QUEUE_PATH . $filename);
  85. $result = 0;
  86. if (file_exists(self::QUEUE_PATH . $filename)) {
  87. $result = 1;
  88. }
  89. } else {
  90. $result = 2;
  91. }
  92. return ($result);
  93. }
  94. /**
  95. * Directly sends email message to recepient using PHP mail function.
  96. *
  97. * @param string $email
  98. * @param string $subj
  99. * @param string $message
  100. *
  101. * @return void
  102. */
  103. public function directPushEmail($email, $subj, $message) {
  104. $sender = __('Ubilling');
  105. $headers = 'From: =?UTF-8?B?' . base64_encode($sender) . '?= <' . $email . ">\n";
  106. $headers .= "MIME-Version: 1.0\n";
  107. $headers .= 'Message-ID: <' . md5(uniqid(time())) . "@" . $sender . ">\n";
  108. $headers .= 'Date: ' . gmdate('D, d M Y H:i:s T', time()) . "\n";
  109. $headers .= "Content-type: text/plain; charset=UTF-8\n";
  110. $headers .= "Content-transfer-encoding: 8bit\n";
  111. $headers .= "X-Mailer: Ubilling\n";
  112. $headers .= "X-MimeOLE: Ubilling\n";
  113. mail($email, '=?UTF-8?B?' . base64_encode($subj) . '?=', $message, $headers);
  114. }
  115. }
  116. ?>