email.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php /** @noinspection PhpUnused */
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. class Email {
  4. /** @var PHPMailer $instance */
  5. private $instance;
  6. private $htmlContent = "";
  7. // Static functions
  8. public static function getPHPMailerInstance() {
  9. $mail = new PHPMailer(true);
  10. switch(MAILER) {
  11. case "smtp":
  12. $mail->isSMTP();
  13. $mail->Host = SMTP_HOST;
  14. $mail->SMTPAuth = true;
  15. $mail->Username = SMTP_USERNAME;
  16. $mail->Password = SMTP_PASSWORD;
  17. $mail->SMTPSecure = SMTP_AUTHENTICATION_TYPE;
  18. $mail->Port = SMTP_PORT;
  19. break;
  20. case "mail":
  21. $mail->isMail();
  22. break;
  23. case "sendmail":
  24. $mail->isSendmail();
  25. break;
  26. }
  27. $mail->CharSet = 'UTF-8';
  28. $mail->Encoding = 'base64';
  29. return $mail;
  30. }
  31. public static function getTextCellHtml($text) {
  32. ob_start();
  33. ?>
  34. <tr>
  35. <td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
  36. <?=htmlspecialchars($text)?>
  37. </td>
  38. </tr>
  39. <?php
  40. return ob_get_clean();
  41. }
  42. // Object functions
  43. public function __construct() {
  44. $this->instance = Email::getPHPMailerInstance();
  45. }
  46. /**
  47. * @param string $template_name
  48. * @throws Exception
  49. */
  50. public function loadHtmlTemplate(string $template_name) {
  51. $this->instance->IsHTML(true);
  52. $path = "emails/$template_name.html";
  53. if(!file_exists($path)) throw new Exception("Email HTML template \"$template_name\" cannot be found.");
  54. $content = file_get_contents($path);
  55. $this->htmlContent = $content;
  56. }
  57. /**
  58. * @param array $values
  59. * @return bool
  60. */
  61. public function setValues(array $values = []) {
  62. foreach($values as $key => $value) {
  63. $this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
  64. }
  65. return true;
  66. }
  67. /**
  68. * @param string $path
  69. * @param string $name
  70. * @param string $encoding
  71. * @param string $type
  72. * @param string $disposition
  73. * @return bool
  74. * @throws \PHPMailer\PHPMailer\Exception
  75. */
  76. public function addAttachment(string $path, string $name = "", string $encoding = "base64", string $type = "", string $disposition = "attachment") {
  77. return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
  78. }
  79. /**
  80. * @param string $email
  81. * @param string $name
  82. * @return bool
  83. * @throws \PHPMailer\PHPMailer\Exception
  84. */
  85. public function setFrom(string $email, string $name = "") {
  86. return $this->instance->setFrom($email, $name);
  87. }
  88. /**
  89. * @param string $email
  90. * @param string $name
  91. * @return bool
  92. */
  93. public function addAddress(string $email, string $name = "") {
  94. return $this->instance->addAddress($email, $name);
  95. }
  96. /**
  97. * @param string $email
  98. * @param string $name
  99. * @return bool
  100. */
  101. public function addReplyTo(string $email, string $name = "") {
  102. return $this->instance->addReplyTo($email, $name);
  103. }
  104. /**
  105. * @param string $subject
  106. * @return bool
  107. */
  108. public function setSubject(string $subject) {
  109. $this->instance->Subject = $subject;
  110. return true;
  111. }
  112. /**
  113. * @param string $string
  114. */
  115. public function setHtmlContent(string $string) {
  116. $this->instance->IsHTML(true);
  117. $this->htmlContent = $string;
  118. }
  119. /**
  120. * @return bool
  121. * @throws Exception
  122. */
  123. public function send(): bool {
  124. // Does our sanity checks and then send email
  125. $instance = $this->instance;
  126. $to_addresses = $instance->getToAddresses();
  127. if(empty($to_addresses)) throw new Exception("No recipients are set.");
  128. if(empty($instance->Subject)) throw new Exception("No subject is set.");
  129. if(empty($this->htmlContent)) throw new Exception("No content is set.");
  130. // Embed images
  131. $match_count = preg_match_all("/src=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
  132. if($match_count > 0) {
  133. $paths = $all_matches[1];
  134. foreach($paths as $path) {
  135. $filename = basename($path);
  136. $cid = str_replace(".", "_", $filename);
  137. $path = ltrim($path, "./");
  138. $this->instance->addEmbeddedImage($path, $cid, $filename);
  139. $this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent);
  140. }
  141. }
  142. $count = preg_match_all("/{([a-z\-]+)}/", $this->htmlContent, $matches);
  143. if($count > 0) {
  144. $missing_keys = [];
  145. foreach($matches[1] as $key) {
  146. $missing_keys[] = $key;
  147. }
  148. throw new Exception("Missing values: " . implode(", ", $missing_keys));
  149. }
  150. $instance->Body = $this->htmlContent;
  151. if(PRODUCTION || defined("DEBUG_MODE")) {
  152. return $this->instance->send();
  153. }
  154. else {
  155. return true;
  156. }
  157. }
  158. }