email.php 4.3 KB

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