| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php /** @noinspection PhpUnused */
- use PHPMailer\PHPMailer\PHPMailer;
- class Email {
- /** @var PHPMailer $instance */
- private $instance;
- private $htmlContent = "";
- // Static functions
- public static function getPHPMailerInstance() {
- $mail = new PHPMailer(true);
- if(defined("SMTP_HOST")) {
- $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;
- }
- else {
- $mail->isMail();
- }
- $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();
- }
- /**
- * @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;
- }
- }
- }
|