email.php 4.2 KB

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