From 20e32c414959caaa2dbd2361fcdc2087f3a1f4c3 Mon Sep 17 00:00:00 2001 From: Victor LABORIE Date: Fri, 9 Mar 2018 13:40:00 +0100 Subject: [PATCH] Add a MailNotify class with usage of Twig --- htdocs/lib/class.ldapdomain.php | 3 +- htdocs/lib/class.ldapserver.php | 2 +- htdocs/lib/class.mailnotify.php | 34 ++++++++++++++++++++++ htdocs/lib/config.php | 7 ++++- htdocs/tpl/mail/account/add_notif.txt.twig | 11 +++++++ htdocs/tpl/mail/account/init.txt.twig | 4 +++ htdocs/tpl/mail/domain/add_notif.txt.twig | 10 +++++++ 7 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 htdocs/lib/class.mailnotify.php create mode 100644 htdocs/tpl/mail/account/add_notif.txt.twig create mode 100644 htdocs/tpl/mail/account/init.txt.twig create mode 100644 htdocs/tpl/mail/domain/add_notif.txt.twig diff --git a/htdocs/lib/class.ldapdomain.php b/htdocs/lib/class.ldapdomain.php index 5d7026c..07714d4 100644 --- a/htdocs/lib/class.ldapdomain.php +++ b/htdocs/lib/class.ldapdomain.php @@ -95,8 +95,7 @@ class LdapDomain extends LdapServer { $error = ldap_error($this->conn); throw new Exception("Erreur dans l'ajout du compte : $error"); } - mail($mail, 'Premier message',"Mail d'initialisation du compte."); - //mailnotify($info,$this->getname(),$password); + MailNotify::addAccount($this->domain, $mail, $name, $password); } public function addAlias($name,$active=false,$mailaccept=array(),$maildrop=array()) { diff --git a/htdocs/lib/class.ldapserver.php b/htdocs/lib/class.ldapserver.php index 004bc7c..80b31bd 100644 --- a/htdocs/lib/class.ldapserver.php +++ b/htdocs/lib/class.ldapserver.php @@ -117,7 +117,7 @@ class LdapServer { throw new Exception("Erreur dans l'ajout du domaine : $error"); } Logger::info('domain '.$name.' added', $this->login); - //domainnotify($name); + MailNotify::addDomain($name); } public function delDomain($name) { diff --git a/htdocs/lib/class.mailnotify.php b/htdocs/lib/class.mailnotify.php new file mode 100644 index 0000000..f2772c9 --- /dev/null +++ b/htdocs/lib/class.mailnotify.php @@ -0,0 +1,34 @@ + false + )); + + self::$adminmail = !empty($config['mail']) ? $config['mail'] : 'root@localhost'; + } + + public static function addDomain($domain) { + $headers = "From: ".self::$adminmail; + + # Notification mail to admin mail + $mail_notif = self::$twig->render('domain/add_notif.txt.twig', array('domain' => $domain)); + mail(self::$adminmail, 'Création du domaine '.$domain, $mail_notif, $headers); + } + + public static function addAccount($domain, $mail, $name, $password) { + $headers = "From: ".self::$adminmail; + + # Welcome mail for account initialization + $mail_init = self::$twig->render('account/init.txt.twig', array('mail' => $mail, 'name' => $name)); + mail($mail, 'Bienvenue !', $mail_init, $headers); + + # Notification mail to admin mail + $mail_notif = self::$twig->render('account/add_notif.txt.twig', array('mail' => $mail, 'name' => $name, 'password' => $password)); + mail(self::$adminmail, 'Création du compte '.$mail, $mail_notif, $headers); + } +} diff --git a/htdocs/lib/config.php b/htdocs/lib/config.php index 78bb8f9..348a053 100644 --- a/htdocs/lib/config.php +++ b/htdocs/lib/config.php @@ -4,9 +4,14 @@ define("VERSION", "2.0.0"); spl_autoload_register(function ($class) { $class = strtolower($class); - include_once("lib/class.$class.php"); + if (file_exists("lib/class.$class.php")) { + require_once("lib/class.$class.php"); + } }); +require_once 'Twig/autoload.php'; + $config = parse_ini_file('../config/config.ini', true); Logger::configure($config['log']); +MailNotify::configure($config['global']); diff --git a/htdocs/tpl/mail/account/add_notif.txt.twig b/htdocs/tpl/mail/account/add_notif.txt.twig new file mode 100644 index 0000000..d8aeab1 --- /dev/null +++ b/htdocs/tpl/mail/account/add_notif.txt.twig @@ -0,0 +1,11 @@ +{# Notification mail send to global config mail address #} +Bonjour, + +Le compte mail "{{ name | escape }}" vient d'être créé. + +Identifiant : {{ mail | escape }} +Mot de passe : {{ password | escape }} + +Cordialement, +-- +L'équipe informatique diff --git a/htdocs/tpl/mail/account/init.txt.twig b/htdocs/tpl/mail/account/init.txt.twig new file mode 100644 index 0000000..4ab3cff --- /dev/null +++ b/htdocs/tpl/mail/account/init.txt.twig @@ -0,0 +1,4 @@ +{# Welcome mail send for account initialization #} +Bienvenue {{ name | escape }}, + +Votre adresse {{ mail | escape }} est maintenant activé. diff --git a/htdocs/tpl/mail/domain/add_notif.txt.twig b/htdocs/tpl/mail/domain/add_notif.txt.twig new file mode 100644 index 0000000..c940be8 --- /dev/null +++ b/htdocs/tpl/mail/domain/add_notif.txt.twig @@ -0,0 +1,10 @@ +{# Notification mail send to global config mail address #} +Bonjour, + +Un nouveau domaine vient d'être créé : {{ domain | escape }} + +Assurez vous que la configuration DNS et MX soit bien en place. + +Cordialement, +-- +L'équipe informatique