Add a MailNotify class with usage of Twig

This commit is contained in:
Victor LABORIE 2018-03-09 13:40:00 +01:00
parent 2a29d73ae9
commit 20e32c4149
7 changed files with 67 additions and 4 deletions

View File

@ -95,8 +95,7 @@ class LdapDomain extends LdapServer {
$error = ldap_error($this->conn); $error = ldap_error($this->conn);
throw new Exception("Erreur dans l'ajout du compte : $error"); throw new Exception("Erreur dans l'ajout du compte : $error");
} }
mail($mail, 'Premier message',"Mail d'initialisation du compte."); MailNotify::addAccount($this->domain, $mail, $name, $password);
//mailnotify($info,$this->getname(),$password);
} }
public function addAlias($name,$active=false,$mailaccept=array(),$maildrop=array()) { public function addAlias($name,$active=false,$mailaccept=array(),$maildrop=array()) {

View File

@ -117,7 +117,7 @@ class LdapServer {
throw new Exception("Erreur dans l'ajout du domaine : $error"); throw new Exception("Erreur dans l'ajout du domaine : $error");
} }
Logger::info('domain '.$name.' added', $this->login); Logger::info('domain '.$name.' added', $this->login);
//domainnotify($name); MailNotify::addDomain($name);
} }
public function delDomain($name) { public function delDomain($name) {

View File

@ -0,0 +1,34 @@
<?php
class MailNotify {
private static $twig, $adminmail;
public static function configure($config) {
$loader = new Twig_Loader_Filesystem('tpl/mail');
self::$twig = new Twig_Environment($loader, array(
'cache' => 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);
}
}

View File

@ -4,9 +4,14 @@ define("VERSION", "2.0.0");
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
$class = strtolower($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); $config = parse_ini_file('../config/config.ini', true);
Logger::configure($config['log']); Logger::configure($config['log']);
MailNotify::configure($config['global']);

View File

@ -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

View File

@ -0,0 +1,4 @@
{# Welcome mail send for account initialization #}
Bienvenue {{ name | escape }},
Votre adresse {{ mail | escape }} est maintenant activé.

View File

@ -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