1
0

email.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * @throws \PHPMailer\PHPMailer\Exception
  96. */
  97. public function addAddress(string $email, string $name = "") {
  98. return $this->instance->addAddress($email, $name);
  99. }
  100. /**
  101. * @param string $email
  102. * @param string $name
  103. * @return bool
  104. * @throws \PHPMailer\PHPMailer\Exception
  105. */
  106. public function addReplyTo(string $email, string $name = "") {
  107. return $this->instance->addReplyTo($email, $name);
  108. }
  109. /**
  110. * @param string $subject
  111. * @return bool
  112. */
  113. public function setSubject(string $subject) {
  114. $this->instance->Subject = $subject;
  115. return true;
  116. }
  117. /**
  118. * @param string $string
  119. */
  120. public function setHtmlContent(string $string) {
  121. $this->instance->IsHTML(true);
  122. $this->htmlContent = $string;
  123. }
  124. /**
  125. * @return bool
  126. * @throws Exception
  127. */
  128. public function send(): bool {
  129. // Does our sanity checks and then send email
  130. $instance = $this->instance;
  131. $to_addresses = $instance->getToAddresses();
  132. if(empty($to_addresses)) throw new Exception("No recipients are set.");
  133. if(empty($instance->Subject)) throw new Exception("No subject is set.");
  134. if(empty($this->htmlContent)) throw new Exception("No content is set.");
  135. // Embed images
  136. $match_count = preg_match_all("/src=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
  137. if($match_count > 0) {
  138. $paths = $all_matches[1];
  139. foreach($paths as $path) {
  140. $filename = basename($path);
  141. $cid = str_replace(".", "_", $filename);
  142. $path = ltrim($path, "./");
  143. $this->instance->addEmbeddedImage($path, $cid, $filename);
  144. $this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent);
  145. }
  146. }
  147. $count = preg_match_all("/{([a-z\-]+)}/", $this->htmlContent, $matches);
  148. if($count > 0) {
  149. $missing_keys = [];
  150. foreach($matches[1] as $key) {
  151. $missing_keys[] = $key;
  152. }
  153. throw new Exception("Missing values: " . implode(", ", $missing_keys));
  154. }
  155. $instance->Body = $this->htmlContent;
  156. return Email::isEnabled() ? $this->instance->send() : true;
  157. }
  158. }