isSMTP(); $mail->Host = SMTP_HOST; $mail->SMTPAuth = true; $mail->Username = SMTP_USERNAME; $mail->Password = SMTP_PASSWORD; $mail->SMTPSecure = SMTP_AUTHENTICATION_TYPE; $mail->Port = SMTP_PORT; return $mail; } public static function getTextCellHtml($text) { ob_start(); ?> instance = Email::getPHPMailerInstance(); } public function loadHtmlTemplate($template_name) { $this->instance->IsHTML(true); $path = "emails/$template_name.html"; if(!file_exists($path)) throw new Exception("Email HTML template \"$template_name\" cannot be found."); $content = file_get_contents($path); $this->htmlContent = $content; } public function setValues($values = []) { foreach($values as $key => $value) { $this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent); } return true; } public function setFrom($email, $name="") { return $this->instance->setFrom($email, $name); } public function addAddress($email, $name="") { return $this->instance->addAddress($email, $name); } public function addReplyTo($email, $name="") { return $this->instance->addReplyTo($email, $name); } public function setSubject($subject) { $this->instance->Subject = $subject; return true; } public function setHtmlContent($string) { $this->instance->IsHTML(true); $this->htmlContent = $string; } public function send() { // Does our sanity checks and then send email $instance = $this->instance; $to_addresses = $instance->getToAddresses(); if(empty($to_addresses)) throw new Exception("No recipients are set."); if(empty($instance->Subject)) throw new Exception("No subject is set."); if(empty($this->htmlContent)) throw new Exception("No content is set."); // Embed images $match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches); if($match_count > 0) { $paths = $all_matches[1]; foreach($paths as $path) { $filename = basename($path); $cid = str_replace(".", "_", $filename); $path = ltrim($path, "./"); $this->instance->addEmbeddedImage($path, $cid, $filename); $this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent); } } $count = preg_match_all("/\{([a-z\-]+)\}/", $this->htmlContent, $matches); if($count > 0) { $missing_keys = []; foreach($matches[1] as $key) { $missing_keys[] = $key; } throw new Exception("Missing values: " . implode(", ", $missing_keys)); } $instance->Body = $this->htmlContent; if(PRODUCTION || DEBUG_MODE) { return $this->instance->send(); } else { return true; } } }