@@ -20,9 +20,6 @@ class UnityMailer
2020 private string $ MSG_PI_APPROVAL_EMAIL ;
2121 private string $ MSG_PI_APPROVAL_NAME ;
2222
23- private \Twig \Loader \FilesystemLoader $ loader ;
24- private \Twig \Environment $ twig ;
25-
2623 public function __construct ()
2724 {
2825 $ this ->MSG_SENDER_EMAIL = CONFIG ["mail " ]["sender " ];
@@ -33,21 +30,6 @@ public function __construct()
3330 $ this ->MSG_ADMIN_NAME = CONFIG ["mail " ]["admin_name " ];
3431 $ this ->MSG_PI_APPROVAL_EMAIL = CONFIG ["mail " ]["pi_approve " ];
3532 $ this ->MSG_PI_APPROVAL_NAME = CONFIG ["mail " ]["pi_approve_name " ];
36- $ this ->loader = new \Twig \Loader \FilesystemLoader ([
37- __DIR__ . "/../../deployment/mail_overrides " ,
38- __DIR__ . "/../mail " ,
39- ]);
40- $ this ->twig = new \Twig \Environment ($ this ->loader , ["strict_variables " => true ]);
41- $ functions = [
42- new TwigFunction ("setSubject " , fn ($ x ) => ($ this ->Subject = $ x )),
43- new TwigFunction ("getRelativeHyperlink " , getRelativeHyperlink (...)),
44- new TwigFunction ("formatHyperlink " , formatHyperlink (...)),
45- new TwigFunction ("throw " , fn ($ x ) => throw new Exception ($ x )),
46- ];
47- foreach ($ functions as $ function ) {
48- $ this ->twig ->addFunction ($ function );
49- }
50- $ this ->twig ->addGlobal ("CONFIG " , CONFIG );
5133 }
5234
5335 public function constructPHPMailer (): PHPMailer
@@ -97,6 +79,25 @@ public function constructPHPMailer(): PHPMailer
9779 return $ mailer ;
9880 }
9981
82+ public function constructTwigEnvironment (): \Twig \Environment
83+ {
84+ $ loader = new \Twig \Loader \FilesystemLoader ([
85+ __DIR__ . "/../../deployment/mail_overrides " ,
86+ __DIR__ . "/../mail " ,
87+ ]);
88+ $ twig = new \Twig \Environment ($ loader , ["strict_variables " => true ]);
89+ $ functions = [
90+ new TwigFunction ("getRelativeHyperlink " , getRelativeHyperlink (...)),
91+ new TwigFunction ("formatHyperlink " , formatHyperlink (...)),
92+ new TwigFunction ("throw " , fn ($ x ) => throw new Exception ($ x )),
93+ ];
94+ foreach ($ functions as $ function ) {
95+ $ twig ->addFunction ($ function );
96+ }
97+ $ twig ->addGlobal ("CONFIG " , CONFIG );
98+ return $ twig ;
99+ }
100+
100101 /**
101102 * @param string|string[] $recipients
102103 * @param ?mixed[] $data
@@ -105,10 +106,14 @@ public function sendMail(string|array $recipients, string $template, ?array $dat
105106 {
106107 $ data ??= [];
107108 $ mailer = $ this ->constructPHPMailer ();
109+
110+ $ twig = $ this ->constructTwigEnvironment ();
111+ $ twig ->addFunction (new TwigFunction ("setSubject " , fn ($ x ) => ($ mailer ->Subject = $ x )));
112+
108113 $ mailer ->setFrom ($ this ->MSG_SENDER_EMAIL , $ this ->MSG_SENDER_NAME );
109114 $ mailer ->addReplyTo ($ this ->MSG_SUPPORT_EMAIL , $ this ->MSG_SUPPORT_NAME );
110115
111- $ mes_html = $ this -> twig ->render ("$ template.html.twig " , $ data );
116+ $ mes_html = $ twig ->render ("$ template.html.twig " , $ data );
112117 $ mailer ->msgHTML ($ mes_html );
113118
114119 if ($ recipients == "admin " ) {
0 commit comments