email.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. class Email {
  4. private $instance;
  5. private $htmlContent = "";
  6. // Static functions
  7. public static function getPHPMailerInstance() {
  8. $mail = new PHPMailer(true);
  9. $mail->isSMTP();
  10. $mail->Host = SMTP_HOST;
  11. $mail->SMTPAuth = true;
  12. $mail->Username = SMTP_USERNAME;
  13. $mail->Password = SMTP_PASSWORD;
  14. $mail->SMTPSecure = SMTP_AUTHENTICATION_TYPE;
  15. $mail->Port = SMTP_PORT;
  16. $mail->CharSet = 'UTF-8';
  17. $mail->Encoding = 'base64';
  18. return $mail;
  19. }
  20. public static function getTextCellHtml($text) {
  21. ob_start();
  22. ?>
  23. <tr>
  24. <td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
  25. <?=htmlspecialchars($text)?>
  26. </td>
  27. </tr>
  28. <?php
  29. return ob_get_clean();
  30. }
  31. // Object functions
  32. public function __construct() {
  33. $this->instance = Email::getPHPMailerInstance();
  34. }
  35. public function loadHtmlTemplate($template_name) {
  36. $this->instance->IsHTML(true);
  37. $path = "emails/$template_name.html";
  38. if(!file_exists($path)) throw new Exception("Email HTML template \"$template_name\" cannot be found.");
  39. $content = file_get_contents($path);
  40. $this->htmlContent = $content;
  41. }
  42. public function setValues($values = []) {
  43. foreach($values as $key => $value) {
  44. $this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
  45. }
  46. return true;
  47. }
  48. public function addAttachment($path, $name = "", $encoding = "base64", $type = "", $disposition = "attachment") {
  49. return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
  50. }
  51. public function setFrom($email, $name="") {
  52. return $this->instance->setFrom($email, $name);
  53. }
  54. public function addAddress($email, $name="") {
  55. return $this->instance->addAddress($email, $name);
  56. }
  57. public function addReplyTo($email, $name="") {
  58. return $this->instance->addReplyTo($email, $name);
  59. }
  60. public function setSubject($subject) {
  61. $this->instance->Subject = $subject;
  62. return true;
  63. }
  64. public function setHtmlContent($string) {
  65. $this->instance->IsHTML(true);
  66. $this->htmlContent = $string;
  67. }
  68. public function send() {
  69. // Does our sanity checks and then send email
  70. $instance = $this->instance;
  71. $to_addresses = $instance->getToAddresses();
  72. if(empty($to_addresses)) throw new Exception("No recipients are set.");
  73. if(empty($instance->Subject)) throw new Exception("No subject is set.");
  74. if(empty($this->htmlContent)) throw new Exception("No content is set.");
  75. // Embed images
  76. $match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
  77. if($match_count > 0) {
  78. $paths = $all_matches[1];
  79. foreach($paths as $path) {
  80. $filename = basename($path);
  81. $cid = str_replace(".", "_", $filename);
  82. $path = ltrim($path, "./");
  83. $this->instance->addEmbeddedImage($path, $cid, $filename);
  84. $this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent);
  85. }
  86. }
  87. $count = preg_match_all("/\{([a-z\-]+)\}/", $this->htmlContent, $matches);
  88. if($count > 0) {
  89. $missing_keys = [];
  90. foreach($matches[1] as $key) {
  91. $missing_keys[] = $key;
  92. }
  93. throw new Exception("Missing values: " . implode(", ", $missing_keys));
  94. }
  95. $instance->Body = $this->htmlContent;
  96. if(PRODUCTION || DEBUG_MODE) {
  97. return $this->instance->send();
  98. }
  99. else {
  100. return true;
  101. }
  102. }
  103. }