| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- use PHPMailer\PHPMailer\PHPMailer;
- class Email {
- private $instance;
- private $htmlContent = "";
- // Static functions
- public static function getPHPMailerInstance() {
- $mail = new PHPMailer(true);
- $mail->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;
- $mail->CharSet = 'UTF-8';
- $mail->Encoding = 'base64';
- return $mail;
- }
- public static function getTextCellHtml($text) {
- ob_start();
- ?>
- <tr>
- <td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
- <?=htmlspecialchars($text)?>
- </td>
- </tr>
- <?php
- return ob_get_clean();
- }
- // Object functions
- public function __construct() {
- $this->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 addAttachment($path, $name = "", $encoding = "base64", $type = "", $disposition = "attachment") {
- return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
- }
- 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;
- }
- }
- }
|