$context Variables transmises au template * * @throws \RuntimeException Si l'envoi échoue * @return void */ public function send(string $to, string $subject, string $template, array $context = []): void { $html = $this->twig->getEnvironment()->render($template, $context); $mail = $this->createMailer(); $mail->addAddress($to); $mail->Subject = $subject; $mail->Body = $html; $mail->AltBody = strip_tags($html); try { $mail->send(); } catch (MailerException $e) { throw new \RuntimeException("Échec de l'envoi de l'email : {$e->getMessage()}", 0, $e); } } /** * Crée et configure une instance PHPMailer prête à l'envoi. * * @return PHPMailer L'instance configurée avec les paramètres SMTP injectés */ private function createMailer(): PHPMailer { $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = $this->host; $mail->SMTPAuth = true; $mail->Username = $this->username; $mail->Password = $this->password; $mail->SMTPSecure = $this->encryption === 'ssl' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = $this->port; $mail->CharSet = PHPMailer::CHARSET_UTF8; $mail->setFrom($this->from, $this->fromName); return $mail; } }