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();
?>
|
=htmlspecialchars($text)?>
|
instance = Email::getPHPMailerInstance();
}
/**
* @param string $template_name
* @throws Exception
*/
public function loadHtmlTemplate(string $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;
}
/**
* @param array $values
* @return bool
*/
public function setValues(array $values = []) {
foreach($values as $key => $value) {
$this->htmlContent = str_replace("{" . $key . "}", $value, $this->htmlContent);
}
return true;
}
/**
* @param string $path
* @param string $name
* @param string $encoding
* @param string $type
* @param string $disposition
* @return bool
* @throws \PHPMailer\PHPMailer\Exception
*/
public function addAttachment(string $path, string $name = "", string $encoding = "base64", string $type = "", string $disposition = "attachment") {
return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
}
/**
* @param string $email
* @param string $name
* @return bool
* @throws \PHPMailer\PHPMailer\Exception
*/
public function setFrom(string $email, string $name = "") {
return $this->instance->setFrom($email, $name);
}
/**
* @param string $email
* @param string $name
* @return bool
*/
public function addAddress(string $email, string $name = "") {
return $this->instance->addAddress($email, $name);
}
/**
* @param string $email
* @param string $name
* @return bool
*/
public function addReplyTo(string $email, string $name = "") {
return $this->instance->addReplyTo($email, $name);
}
/**
* @param string $subject
* @return bool
*/
public function setSubject(string $subject) {
$this->instance->Subject = $subject;
return true;
}
/**
* @param string $string
*/
public function setHtmlContent(string $string) {
$this->instance->IsHTML(true);
$this->htmlContent = $string;
}
/**
* @return bool
* @throws Exception
*/
public function send(): bool {
// 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;
}
}
}