From 7cbb6e47e696f825601a0496b484ed72f89a3c94 Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Wed, 31 Dec 2025 07:32:06 +0100 Subject: [PATCH] Added mailcow support Signed-off-by: Janos SUTO --- config.php.in | 4 +++ webui/model/user/auth.php | 72 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/config.php.in b/config.php.in index 49a8f4db..568e745c 100644 --- a/config.php.in +++ b/config.php.in @@ -185,6 +185,10 @@ $config['GOOGLE_DEVELOPER_KEY'] = 'xxxxxxxxxxxx'; $config['GOOGLE_APPLICATION_NAME'] = 'piler enterprise email archiver'; $config['GOOGLE_ALL_MAIL'] = '[Gmail]/All Mail'; +// Mailcow + +$config['MAILCOW_API_KEY'] = ''; +$config['MAILCOW_HOST'] = ''; $config['ENABLE_AUDIT'] = 1; $config['MEMCACHED_ENABLED'] = 0; diff --git a/webui/model/user/auth.php b/webui/model/user/auth.php index 10555273..b6705997 100644 --- a/webui/model/user/auth.php +++ b/webui/model/user/auth.php @@ -405,6 +405,12 @@ private function checkLoginAgainstIMAP($imap_server = array(), $username = '', $ if($imap->login($login, $password)) { $imap->logout(); + if(MAILCOW_API_KEY) { + $userinfo = $this->get_mailcow_userinfo($username); + $emails = $userinfo['emails']; + $realname = $userinfo['realname']; + } + if(CUSTOM_EMAIL_QUERY_FUNCTION && function_exists(CUSTOM_EMAIL_QUERY_FUNCTION)) { $emails = call_user_func(CUSTOM_EMAIL_QUERY_FUNCTION, $username); } @@ -648,6 +654,68 @@ public function escapeLdapFilter($str = '') { return str_replace($metaChars, $quotedMetaChars, $str); } -} -?> + + private function mailcow_query($path = '') { + if($path === '') { return []; } + + $ch = curl_init(); + + $uri = MAILCOW_HOST . $path; + + $headers = [ + 'Accept: application/json', + 'X-API-Key: ' . MAILCOW_API_KEY + ]; + + curl_setopt($ch, CURLOPT_URL, $uri); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + if(ENABLE_SSL_VERIFY == 0) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } + + $ret = curl_exec($ch); + + curl_close($ch); + + $ret = json_decode($ret, true); + + return $ret; + } + + + public function get_mailcow_userinfo($username = '') { + $emails = [$username]; + + // Get all aliases + $aliases = $this->mailcow_query('/api/v1/get/alias/all'); + + // TODO: Cache the results? + + foreach($aliases as $alias) { + if(isset($alias['active']) && $alias['active'] !== 1) { + continue; + } + + if(isset($alias['active_int']) && $alias['active_int'] !== 1) { + continue; + } + + array_push($emails, strtolower($alias['address'])); + + //syslog(LOG_INFO, 'mailcow alias: ' . $alias['address']); + } + + // Get user's real name + + $user = $this->mailcow_query('/api/v1/get/mailbox/' . $username); + + return [ + 'realname' => $user['name'], + 'emails' => $emails + ]; + } +}