Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Updating Postfix virtual alias maps from Horde.
- *
- * Horde allows users to have multiple "identities" and email aliases under
- * those identities. When running a personal mail server, it's more than
- * likely that all of those email addresses should be deliverable. This script
- * takes all of the aliases in Horde's database and rewrites the virtual alias
- * maps and domains files using the identities setup in Horde. Additionally,
- * it will only update the files and reload postfix if a file has changed, so
- * it is safe to run as a cron job as often as needed.
- *
- * Note that this script was written for Postfix on CentOS 6.4, and therefore
- * you may need to update the locations of the "postmap" and "service"
- * executables at the end of this script if you use a distribution other than
- * CentOS. If you use a mail server other than postscript, if that mail server
- * has the concept of alias maps/domains, then you may still be able to use
- * this script, however it will need to be modified to use a format specific
- * to that application. You may also need to update the locations of the maps
- * and domains file at the beginning of this script if you have an existing
- * setup with those files in different locations, or if you just want to put
- * these files somewhere else. Additionally, you WILL need to update the MySQL
- * connection information at the beginning of this script with your own.
- *
- * @author Ryan Petris <[email protected]>
- * @copyright (c) 2013 Ryan Petris
- * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
- */
- $db = new PDO("mysql:host=localhost;dbname=horde", "username", "password");
- $maps_file = "/etc/postfix/virtual_alias_maps";
- $domains_file = "/etc/postfix/virtual_alias_domains";
- $stmt = $db->prepare("SELECT * FROM horde_prefs WHERE pref_name = 'identities';");
- $stmt->execute();
- $prefs = $stmt->fetchAll(PDO::FETCH_ASSOC);
- $emails = array();
- $email_lines = array();
- $domains = array();
- if (!count($prefs)) {
- die("Nothing returned from database query!");
- }
- foreach ($prefs as $pref) {
- if (!array_key_exists($pref["pref_uid"], $emails)) {
- $emails[$pref["pref_uid"]] = array();
- }
- $items = unserialize($pref["pref_value"]);
- foreach ($items as $item) {
- if (!empty($item["from_addr"]) && !in_array($item["from_addr"], $emails[$pref["pref_uid"]])) {
- $emails[$pref["pref_uid"]][] = $item["from_addr"];
- }
- if (array_key_exists("alias_addr", $item)) {
- foreach ($item["alias_addr"] as $alias) {
- if (!empty($alias) && !in_array($alias, $emails[$pref["pref_uid"]])) {
- $emails[$pref["pref_uid"]][] = $alias;
- }
- }
- }
- }
- }
- foreach ($emails as $user => $useremails) {
- foreach ($useremails as $email) {
- $email_lines[] = $email . " " . $user;
- $parts = explode('@', $email);
- if ($parts) {
- $domain = $parts[count($parts) - 1];
- if (!in_array($domain, $domains)) {
- $domains[] = $domain;
- }
- }
- }
- }
- $update = false;
- $maps_contents = file_get_contents($maps_file);
- $domains_contents = file_get_contents($domains_file);
- $maps_contents_new = implode("\n", $email_lines) . "\n";
- $domains_contents_new = implode("\n", $domains) . "\n";
- if ($maps_contents != $maps_contents_new || $domains_contents != $domains_contents_new) {
- file_put_contents($maps_file, $maps_contents_new);
- file_put_contents($domains_file, $domains_contents_new);
- exec("/usr/sbin/postmap " . $maps_file);
- exec("/sbin/service postfix reload");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement