api.ubillingphpmail.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. require_once ('api/vendor/PHPMailer/PHPMailer.php');
  3. require_once ('api/vendor/PHPMailer/OAuth.php');
  4. require_once ('api/vendor/PHPMailer/SMTP.php');
  5. require_once ('api/vendor/PHPMailer/Exception.php');
  6. //Import PHPMailer classes
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\SMTP;
  9. use PHPMailer\PHPMailer\OAuth;
  10. use PHPMailer\PHPMailer\Exception;
  11. /**
  12. * Ubilling email sending based on phpmail class
  13. */
  14. class UbillingPHPMail {
  15. /**
  16. * Enable SMTP debugging
  17. * SMTP::DEBUG_OFF = off (for production use)
  18. * SMTP::DEBUG_CLIENT = client messages
  19. * SMTP::DEBUG_SERVER = client and server messages
  20. *
  21. * @var string
  22. */
  23. protected $mailerDebug = 'DEBUG_OFF';
  24. /**
  25. * Address/hostname of the remote SMTP server that will be used to send messages
  26. *
  27. * @var string
  28. */
  29. protected $mailerSMTPHost = 'smtp.mail.server';
  30. /**
  31. * SMTP port to use to connect to remote SMTP server
  32. *
  33. * @var string
  34. */
  35. protected $mailerSMTPPort = '25';
  36. /**
  37. * Encryption type to use for SMTP connection
  38. * empty - encryption off, ENCRYPTION_SMTPS - ssl, ENCRYPTION_STARTTLS - tls
  39. *
  40. * @var string
  41. */
  42. protected $mailerSMTPSecure = 'ENCRYPTION_SMTPS';
  43. /**
  44. * SMTP authentication on/off
  45. *
  46. * @var bool
  47. */
  48. protected $mailerSMTPAuth = true;
  49. /**
  50. * Login used to authenticate on SMTP server
  51. *
  52. * @var string
  53. */
  54. protected $mailerSMTPUser = '';
  55. /**
  56. * Password used to authenticate on SMTP server
  57. *
  58. * @var string
  59. */
  60. protected $mailerSMTPPasswd = '';
  61. /**
  62. * Will be used in email <From> field if not specified in message itself
  63. *
  64. * @var string
  65. */
  66. protected $mailerSMTPDefaultFrom = '';
  67. /**
  68. * Placeholder for PHPMailer object
  69. *
  70. * @var null
  71. */
  72. public $phpMailer = null;
  73. /**
  74. * Contains path to the attachments directory
  75. *
  76. * @var string
  77. */
  78. public $mailerAttachPath = 'exports/';
  79. /**
  80. * Contains PHPMailer e-mail messages path
  81. */
  82. const QUEUE_PATH = 'content/phpmailqueue/';
  83. /**
  84. * Creates new PHPMail queue class instance
  85. */
  86. public function __construct() {
  87. $this->mailerDebug = zb_StorageGet('SENDDOG_PHPMAILER_DEBUG');
  88. $this->mailerSMTPHost = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_HOST');
  89. $this->mailerSMTPPort = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_PORT');
  90. $this->mailerSMTPSecure = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_SECURE');
  91. $this->mailerSMTPAuth = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_USEAUTH');
  92. $this->mailerSMTPUser = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_USER');
  93. $this->mailerSMTPPasswd = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_PASSWD');
  94. $this->mailerSMTPDefaultFrom = zb_StorageGet('SENDDOG_PHPMAILER_SMTP_DEFAULTFROM');
  95. $this->mailerAttachPath = zb_StorageGet('SENDDOG_PHPMAILER_ATTACHMENTS_PATH');
  96. $this->phpMailer = $this->initPHPMailer();
  97. }
  98. /**
  99. * QUEUE_PATH getter
  100. *
  101. * @return string
  102. */
  103. public function getQueuePath() {
  104. return (self::QUEUE_PATH);
  105. }
  106. /**
  107. * Inits and returns a PHPMailer object instance
  108. *
  109. * @return PHPMailer
  110. */
  111. public function initPHPMailer() {
  112. //Create a new PHPMailer instance
  113. $mail = new PHPMailer;
  114. //Tell PHPMailer to use SMTP
  115. $mail->isSMTP();
  116. //Tell PHPMailer to use UTF-8 encoding
  117. $mail->CharSet = "UTF-8";
  118. $mail->Debugoutput = 'error_log';
  119. //Enable SMTP debugging
  120. // SMTP::DEBUG_OFF = off (for production use)
  121. // SMTP::DEBUG_CLIENT = client messages
  122. // SMTP::DEBUG_SERVER = client and server messages
  123. switch ($this->mailerDebug) {
  124. case 1:
  125. $mail->SMTPDebug = SMTP::DEBUG_OFF;
  126. break;
  127. case 2:
  128. $mail->SMTPDebug = SMTP::DEBUG_CLIENT;
  129. break;
  130. case 3:
  131. $mail->SMTPDebug = SMTP::DEBUG_SERVER;
  132. break;
  133. default:
  134. $mail->SMTPDebug = SMTP::DEBUG_OFF;
  135. }
  136. //Set the hostname of the mail server
  137. $mail->Host = $this->mailerSMTPHost;
  138. // use
  139. // $mail->Host = gethostbyname('smtp.gmail.com');
  140. // if your network does not support SMTP over IPv6
  141. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  142. $mail->Port = $this->mailerSMTPPort;
  143. //Set the encryption mechanism to use - STARTTLS or SMTPS
  144. //$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  145. switch ($this->mailerSMTPSecure) {
  146. case 1:
  147. $mail->SMTPSecure = '';
  148. $mail->SMTPAutoTLS = false;
  149. break;
  150. case 2:
  151. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  152. break;
  153. case 3:
  154. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  155. break;
  156. default:
  157. $mail->SMTPSecure = '';
  158. $mail->SMTPAutoTLS = false;
  159. }
  160. //Whether to use SMTP authentication
  161. $mail->SMTPAuth = $this->mailerSMTPAuth;
  162. //Username to use for SMTP authentication - use full email address for gmail
  163. $mail->Username = $this->mailerSMTPUser;
  164. //Password to use for SMTP authentication
  165. $mail->Password = $this->mailerSMTPPasswd;
  166. return ($mail);
  167. }
  168. /**
  169. * Stores message in email sending queue. Use this method in your modules.
  170. *
  171. * @param string $email
  172. * @param string $subject
  173. * @param string $message
  174. * @param string $attachPath
  175. * @param bool $bodyAsHTML
  176. * @param string $fromField
  177. * @param array $customHeaders
  178. * @param string $module
  179. *
  180. * @return bool
  181. */
  182. public function sendEmail($email, $subject, $message, $attachPath = '', $bodyAsHTML = false, $fromField = '', $customHeaders = array(), $module = '') {
  183. $result = false;
  184. $email = trim($email);
  185. $subject = trim($subject);
  186. $module = (!empty($module)) ? ' MODULE ' . $module : '';
  187. if (!empty($email)) {
  188. $message = trim($message);
  189. $filename = self::QUEUE_PATH . 'emlpm_' . zb_rand_string(8);
  190. $storedata['email'] = $email;
  191. $storedata['subj'] = $subject;
  192. $storedata['message'] = $message;
  193. $storedata['attachpath'] = $attachPath;
  194. $storedata['bodyashtml'] = $bodyAsHTML;
  195. $storedata['from'] = $fromField;
  196. $storedata['customheaders'] = array();
  197. if (!empty($customHeaders)) {
  198. foreach ($customHeaders as $eachHeader) {
  199. $storedata['customheaders'][] = $eachHeader;
  200. }
  201. }
  202. $storedata = json_encode($storedata);
  203. file_put_contents($filename, $storedata);
  204. log_register('UPHPEML SEND EMAIL `' . $email . '`' . $module);
  205. $result = true;
  206. }
  207. return ($result);
  208. }
  209. /**
  210. * Returns count of emails available in queue
  211. *
  212. * @return int
  213. */
  214. public function getQueueCount() {
  215. $messagesQueueCount = rcms_scandir(self::QUEUE_PATH);
  216. $result = sizeof($messagesQueueCount);
  217. return ($result);
  218. }
  219. /**
  220. * Returns array containing all emails queue data as index=>data
  221. *
  222. * @return array
  223. */
  224. public function getQueueData() {
  225. $result = array();
  226. $messagesQueue = rcms_scandir(self::QUEUE_PATH);
  227. if (!empty($messagesQueue)) {
  228. foreach ($messagesQueue as $io => $eachmessage) {
  229. $messageDate = date("Y-m-d H:i:s", filectime(self::QUEUE_PATH . $eachmessage));
  230. $messageData = file_get_contents(self::QUEUE_PATH . $eachmessage);
  231. $messageData = json_decode($messageData, true);
  232. $result[$io]['filename'] = $eachmessage;
  233. $result[$io]['date'] = $messageDate;
  234. $result[$io]['email'] = $messageData['email'];
  235. $result[$io]['subj'] = $messageData['subj'];
  236. $result[$io]['message'] = $messageData['message'];
  237. $result[$io]['attachpath'] = $messageData['attachpath'];
  238. $result[$io]['bodyashtml'] = $messageData['bodyashtml'];
  239. $result[$io]['from'] = $messageData['from'];
  240. $result[$io]['customheaders'] = $messageData['customheaders'];
  241. }
  242. }
  243. return ($result);
  244. }
  245. /**
  246. * Deletes message from local queue
  247. *
  248. * @param string $filename Existing message filename
  249. *
  250. * @return int 0 - ok, 1 - deletion unsuccessful, 2 - file not found
  251. */
  252. public function deleteEmail($filename) {
  253. if (file_exists(self::QUEUE_PATH . $filename)) {
  254. rcms_delete_files(self::QUEUE_PATH . $filename);
  255. $result = 0;
  256. if (file_exists(self::QUEUE_PATH . $filename)) {
  257. $result = 1;
  258. }
  259. } else {
  260. $result = 2;
  261. }
  262. return ($result);
  263. }
  264. /**
  265. * Deletes attachment after sending
  266. *
  267. * @param $filename
  268. *
  269. * @return int 0 - ok, 1 - deletion unsuccessful, 2 - file not found
  270. */
  271. public function deleteAttachment($filename) {
  272. if (empty($filename)) {
  273. $result = 2;
  274. } else {
  275. if (file_exists($this->mailerAttachPath . $filename)) {
  276. rcms_delete_files($this->mailerAttachPath . $filename);
  277. $result = 0;
  278. if (file_exists($this->mailerAttachPath . $filename)) {
  279. $result = 1;
  280. }
  281. } else {
  282. $result = 2;
  283. }
  284. }
  285. return ($result);
  286. }
  287. /**
  288. * Directly sends email message to recepient using PHP mail function.
  289. *
  290. * @param string $email
  291. * @param string $subject
  292. * @param string $message
  293. * @param string $attachPath
  294. * @param bool $bodyAsHTML
  295. * @param string $from
  296. * @param array $customHeaders
  297. *
  298. * @return void
  299. */
  300. public function directPushEmail($email, $subject, $message, $attachPath, $bodyAsHTML = false, $from = '', $customHeaders = array()) {
  301. $fromField = (empty($from)) ? $this->mailerSMTPDefaultFrom : $from;
  302. //Set who the message is to be sent from
  303. $this->phpMailer->setFrom($fromField);
  304. //Set an alternative reply-to address
  305. //$this->phpMailer->addReplyTo('replyto@example.com', 'First Last');
  306. //Set who the message is to be sent to
  307. $this->phpMailer->addAddress($email);
  308. //Set the subject line
  309. $this->phpMailer->Subject = $subject;
  310. //Read an HTML message body from an external file, convert referenced images to embedded,
  311. //convert HTML into a basic plain-text alternative body
  312. //$this->phpMailer->msgHTML(file_get_contents('content/documents/invoice_test2.html'), __DIR__);
  313. $this->phpMailer->Body = $message;
  314. if ($bodyAsHTML) {
  315. $this->phpMailer->IsHTML(true);
  316. }
  317. if (!empty($customHeaders)) {
  318. //$this->phpMailer->addCustomHeader('MIME-Version: 1.0' . "\r\n");
  319. //$this->phpMailer->addCustomHeader('Content-type: text/html; charset=iso-8859-1' . "\r\n");
  320. foreach ($customHeaders as $eachHeader) {
  321. $this->phpMailer->addCustomHeader($eachHeader);
  322. }
  323. }
  324. //Attach a file
  325. if (!empty($attachPath)) {
  326. try {
  327. $this->phpMailer->addAttachment($attachPath);
  328. } catch (\Exception $ex) {
  329. log_register('UPHPEML Error: ' . $ex->getMessage() . ' | ' . $ex->getTraceAsString());
  330. }
  331. }
  332. //send the message, check for errors
  333. if (!$this->phpMailer->send()) {
  334. log_register('UPHPEML Error: ' . $this->phpMailer->ErrorInfo);
  335. }
  336. }
  337. }