1
0

email.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Embed images
  39. $match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $content, $all_matches);
  40. if($match_count > 0) {
  41. $paths = $all_matches[1];
  42. foreach($paths as $path) {
  43. $filename = basename($path);
  44. $cid = str_replace(".", "_", $filename);
  45. $path = ltrim($path, "./");
  46. $this->instance->addEmbeddedImage($path, $cid, $filename);
  47. $content = str_replace("../$path", "cid:$cid", $content);
  48. }
  49. }
  50. $this->htmlContent = $content;
  51. }
  52. public function setValues($values = []) {
  53. foreach($values as $key => $value) {
  54. $this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
  55. }
  56. return true;
  57. }
  58. public function setFrom($email, $name="") {
  59. return $this->instance->setFrom($email, $name);
  60. }
  61. public function addAddress($email, $name="") {
  62. return $this->instance->addAddress($email, $name);
  63. }
  64. public function addReplyTo($email, $name="") {
  65. return $this->instance->addReplyTo($email, $name);
  66. }
  67. public function setSubject($subject) {
  68. $this->instance->Subject = $subject;
  69. return true;
  70. }
  71. public function send() {
  72. // Does our sanity checks and then send email
  73. $instance = $this->instance;
  74. $to_addresses = $instance->getToAddresses();
  75. if(empty($to_addresses)) throw new Exception("No recipients are set.");
  76. if(empty($instance->Subject)) throw new Exception("No subject is set.");
  77. if(empty($this->htmlContent)) throw new Exception("No content is set.");
  78. $count = preg_match_all("/\{([a-z\-]+)\}/", $this->htmlContent, $matches);
  79. if($count > 0) {
  80. $missing_keys = [];
  81. foreach($matches[1] as $key) {
  82. $missing_keys[] = $key;
  83. }
  84. throw new Exception("Missing values: " . implode(", ", $missing_keys));
  85. }
  86. $instance->Body = $this->htmlContent;
  87. if(PRODUCTION || DEBUG_MODE) {
  88. return $this->instance->send();
  89. }
  90. else {
  91. return true;
  92. }
  93. }
  94. }