email.php 3.0 KB

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