email.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public static function isEnabled(): bool {
  43. return PRODUCTION || defined("DEBUG_MODE");
  44. }
  45. // Object functions
  46. public function __construct() {
  47. $this->instance = Email::getPHPMailerInstance();
  48. }
  49. /**
  50. * @param string $template_name
  51. * @throws Exception
  52. */
  53. public function loadHtmlTemplate(string $template_name) {
  54. $this->instance->IsHTML(true);
  55. $path = "emails/$template_name.html";
  56. if(!file_exists($path)) throw new Exception("Email HTML template \"$template_name\" cannot be found.");
  57. $content = file_get_contents($path);
  58. $this->htmlContent = $content;
  59. }
  60. /**
  61. * @param array $values
  62. * @return bool
  63. */
  64. public function setValues(array $values = []) {
  65. foreach($values as $key => $value) {
  66. $this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
  67. }
  68. return true;
  69. }
  70. /**
  71. * @param string $path
  72. * @param string $name
  73. * @param string $encoding
  74. * @param string $type
  75. * @param string $disposition
  76. * @return bool
  77. * @throws \PHPMailer\PHPMailer\Exception
  78. */
  79. public function addAttachment(string $path, string $name = "", string $encoding = "base64", string $type = "", string $disposition = "attachment") {
  80. return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
  81. }
  82. /**
  83. * @param string $email
  84. * @param string $name
  85. * @return bool
  86. * @throws \PHPMailer\PHPMailer\Exception
  87. */
  88. public function setFrom(string $email, string $name = "") {
  89. return $this->instance->setFrom($email, $name);
  90. }
  91. /**
  92. * @param string $email
  93. * @param string $name
  94. * @return bool
  95. */
  96. public function addAddress(string $email, string $name = "") {
  97. return $this->instance->addAddress($email, $name);
  98. }
  99. /**
  100. * @param string $email
  101. * @param string $name
  102. * @return bool
  103. */
  104. public function addReplyTo(string $email, string $name = "") {
  105. return $this->instance->addReplyTo($email, $name);
  106. }
  107. /**
  108. * @param string $subject
  109. * @return bool
  110. */
  111. public function setSubject(string $subject) {
  112. $this->instance->Subject = $subject;
  113. return true;
  114. }
  115. /**
  116. * @param string $string
  117. */
  118. public function setHtmlContent(string $string) {
  119. $this->instance->IsHTML(true);
  120. $this->htmlContent = $string;
  121. }
  122. /**
  123. * @return bool
  124. * @throws Exception
  125. */
  126. public function send(): bool {
  127. // Does our sanity checks and then send email
  128. $instance = $this->instance;
  129. $to_addresses = $instance->getToAddresses();
  130. if(empty($to_addresses)) throw new Exception("No recipients are set.");
  131. if(empty($instance->Subject)) throw new Exception("No subject is set.");
  132. if(empty($this->htmlContent)) throw new Exception("No content is set.");
  133. // Embed images
  134. $match_count = preg_match_all("/src=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
  135. if($match_count > 0) {
  136. $paths = $all_matches[1];
  137. foreach($paths as $path) {
  138. $filename = basename($path);
  139. $cid = str_replace(".", "_", $filename);
  140. $path = ltrim($path, "./");
  141. $this->instance->addEmbeddedImage($path, $cid, $filename);
  142. $this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent);
  143. }
  144. }
  145. $count = preg_match_all("/{([a-z\-]+)}/", $this->htmlContent, $matches);
  146. if($count > 0) {
  147. $missing_keys = [];
  148. foreach($matches[1] as $key) {
  149. $missing_keys[] = $key;
  150. }
  151. throw new Exception("Missing values: " . implode(", ", $missing_keys));
  152. }
  153. $instance->Body = $this->htmlContent;
  154. return Email::isEnabled() ? $this->instance->send() : true;
  155. }
  156. }