Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions resources/lib/UnityMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use Exception;
use Twig\TwigFunction;

class UnityMailerException extends Exception {}

/**
* This is a class that uses PHPmailer to send emails based on templates
*/
class UnityMailer extends PHPMailer
class UnityMailer
{
private string $MSG_SENDER_EMAIL;
private string $MSG_SENDER_NAME;
Expand All @@ -22,14 +20,8 @@ class UnityMailer extends PHPMailer
private string $MSG_PI_APPROVAL_EMAIL;
private string $MSG_PI_APPROVAL_NAME;

private \Twig\Loader\FilesystemLoader $loader;
private \Twig\Environment $twig;

public function __construct()
{
parent::__construct();
$this->isSMTP();

$this->MSG_SENDER_EMAIL = CONFIG["mail"]["sender"];
$this->MSG_SENDER_NAME = CONFIG["mail"]["sender_name"];
$this->MSG_SUPPORT_EMAIL = CONFIG["mail"]["support"];
Expand All @@ -38,15 +30,22 @@ public function __construct()
$this->MSG_ADMIN_NAME = CONFIG["mail"]["admin_name"];
$this->MSG_PI_APPROVAL_EMAIL = CONFIG["mail"]["pi_approve"];
$this->MSG_PI_APPROVAL_NAME = CONFIG["mail"]["pi_approve_name"];
}
Comment thread
simonLeary42 marked this conversation as resolved.

private function constructPHPMailer(): PHPMailer
{
$mailer = new PHPMailer(exceptions: true);
$mailer->isSMTP();

if (empty(CONFIG["smtp"]["host"])) {
throw new Exception("SMTP server hostname not set");
}
$this->Host = CONFIG["smtp"]["host"];
$mailer->Host = CONFIG["smtp"]["host"];

if (empty(CONFIG["smtp"]["port"])) {
throw new Exception("SMTP server port not set");
}
$this->Port = CONFIG["smtp"]["port"];
$mailer->Port = CONFIG["smtp"]["port"];

$security = CONFIG["smtp"]["security"];
$security_conf_valid = empty($security) || $security == "tls" || $security == "ssl";
Expand All @@ -55,44 +54,48 @@ public function __construct()
"SMTP security is not set correctly, leave empty, use 'tls', or 'ssl'",
);
}
Comment thread
simonLeary42 marked this conversation as resolved.
$this->SMTPSecure = $security;
$mailer->SMTPSecure = $security;

if (!empty(CONFIG["smtp"]["user"])) {
$this->SMTPAuth = true;
$this->Username = CONFIG["smtp"]["user"];
$mailer->SMTPAuth = true;
$mailer->Username = CONFIG["smtp"]["user"];
} else {
$this->SMTPAuth = false;
$mailer->SMTPAuth = false;
}

if (!empty(CONFIG["smtp"]["pass"])) {
$this->Password = CONFIG["smtp"]["pass"];
$mailer->Password = CONFIG["smtp"]["pass"];
}

if (CONFIG["smtp"]["ssl_verify"] == "false") {
$this->SMTPOptions = [
$mailer->SMTPOptions = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true,
],
];
}
return $mailer;
}

$this->loader = new \Twig\Loader\FilesystemLoader([
private function constructTwigEnvironment(): \Twig\Environment
{
$loader = new \Twig\Loader\FilesystemLoader([
__DIR__ . "/../../deployment/mail_overrides",
__DIR__ . "/../mail",
]);
$this->twig = new \Twig\Environment($this->loader, ["strict_variables" => true]);
$twig = new \Twig\Environment($loader, ["strict_variables" => true]);
$functions = [
new TwigFunction("setSubject", fn($x) => ($this->Subject = $x)),
new TwigFunction("getRelativeHyperlink", getRelativeHyperlink(...)),
new TwigFunction("formatHyperlink", formatHyperlink(...)),
new TwigFunction("throw", fn($x) => throw new Exception($x)),
];
foreach ($functions as $function) {
$this->twig->addFunction($function);
$twig->addFunction($function);
}
$this->twig->addGlobal("CONFIG", CONFIG);
$twig->addGlobal("CONFIG", CONFIG);
return $twig;
}

/**
Expand All @@ -101,31 +104,31 @@ public function __construct()
*/
public function sendMail(string|array $recipients, string $template, ?array $data = null): void
{
$mailer = $this->constructPHPMailer();

$twig = $this->constructTwigEnvironment();
$twig->addFunction(new TwigFunction("setSubject", fn($x) => ($mailer->Subject = $x)));

$data ??= [];
$this->setFrom($this->MSG_SENDER_EMAIL, $this->MSG_SENDER_NAME);
$this->addReplyTo($this->MSG_SUPPORT_EMAIL, $this->MSG_SUPPORT_NAME);
$mailer->setFrom($this->MSG_SENDER_EMAIL, $this->MSG_SENDER_NAME);
$mailer->addReplyTo($this->MSG_SUPPORT_EMAIL, $this->MSG_SUPPORT_NAME);
Comment thread
simonLeary42 marked this conversation as resolved.

$mes_html = $this->twig->render("$template.html.twig", $data);
$this->msgHTML($mes_html);
$mes_html = $twig->render("$template.html.twig", $data);
$mailer->msgHTML($mes_html);

if ($recipients == "admin") {
$this->addBCC($this->MSG_ADMIN_EMAIL, $this->MSG_ADMIN_NAME);
$mailer->addBCC($this->MSG_ADMIN_EMAIL, $this->MSG_ADMIN_NAME);
} elseif ($recipients == "pi_approve") {
$this->addBCC($this->MSG_PI_APPROVAL_EMAIL, $this->MSG_PI_APPROVAL_NAME);
$mailer->addBCC($this->MSG_PI_APPROVAL_EMAIL, $this->MSG_PI_APPROVAL_NAME);
} else {
if (is_array($recipients)) {
foreach ($recipients as $addr) {
$this->addBCC($addr);
$mailer->addBCC($addr);
}
} else {
$this->addAddress($recipients);
$mailer->addAddress($recipients);
}
}

$output = parent::send();
if ($output === false) {
throw new UnityMailerException($this->ErrorInfo);
}
$this->clearAllRecipients();
$mailer->send();
}
}
Loading