(09-24-2018, 09:50 AM)wormsunited Wrote: I think that sounds amazing! If you can post the code we can update that asap i think in everyone's interest and a major contribution to the community too.
Ok, here's what I changed - and I take no credit for these changes as they are based on code that exists in a php file that doesn't seem to trigger anymore - possibly left over from the ZPanel days?
There are four mail modules, and I decided that whenever an entry is deleted in any of them (mailbox, alias, forwarder or distribution lists) I want to check the database and see if there are any other mailboxes or aliases still using that domain. If not, I want to delete the domain from the Postfix database so Postfix does not use it to route mail to a local mailbox. I don't restart Postfix as I don't think it's necessary (but would be happy if others could confirm this).
The paths I give here are based on the repository paths (e.g. in the sentora-core repository on GitHub, you can go straight into the modules directory, but on my live CentOS servers the path to the modules directory is /etc/sentora/panel/modules).
Mailboxes Module
Edit modules/mailboxes/code/postfix.php and before the final } of the // Deleting PostFix Mailboxes function add:
Code:
// If no more mailboxes or aliases for the domain exist, delete the domain to
// prevent Postfix using a local route when sending to this domain in future
$domaincheck = explode("@", $rowmailbox['mb_address_vc']);
$sql = $mail_db->prepare("SELECT * FROM mailbox WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$mailboxresult = $sql->fetch();
$sql = $mail_db->prepare("SELECT * FROM alias WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$aliasresult = $sql->fetch();
if (!$mailboxresult && !$aliasresult) {
$sql = $mail_db->prepare("DELETE FROM domain WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
}
Aliases Module
Edit modules/aliases/code/postfix.php and before the final } of the // Deleting Postfix Alias function add:
Code:
// If no more mailboxes or aliases for the domain exist, delete the domain to
// prevent Postfix using a local route when sending to this domain in future
$domaincheck = explode("@", $rowalias['al_address_vc']);
$sql = $mail_db->prepare("SELECT * FROM mailbox WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$mailboxresult = $sql->fetch();
$sql = $mail_db->prepare("SELECT * FROM alias WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$aliasresult = $sql->fetch();
if (!$mailboxresult && !$aliasresult) {
$sql = $mail_db->prepare("DELETE FROM domain WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
}
Forwarders Module
Edit modules/forwarders/code/postfix.php, ignore the fact that functions refer to "hmail" instead of "Postfix" (that's left over from Windows when hMailServer was used!) and before the final } of the // Deleting hMail Forwarder function add:
Code:
// If no more mailboxes or aliases for the domain exist, delete the domain to
// prevent Postfix using a local route when sending to this domain in future
$domaincheck = explode("@", $rowforwarder['fw_address_vc']);
$sql = $mail_db->prepare("SELECT * FROM mailbox WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$mailboxresult = $sql->fetch();
$sql = $mail_db->prepare("SELECT * FROM alias WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$aliasresult = $sql->fetch();
if (!$mailboxresult && !$aliasresult) {
$sql = $mail_db->prepare("DELETE FROM domain WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
}
Distribution Lists Module
Edit modules/distlists/code/postfix.php, ignore the fact that "Distubution" is mis-spelt, and before the final } of the // Deleting Postfix Distubution List function add:
Code:
// If no more mailboxes or aliases for the domain exist, delete the domain to
// prevent Postfix using a local route when sending to this domain in future
$domaincheck = explode("@", $rowdl['dl_address_vc']);
$sql = $mail_db->prepare("SELECT * FROM mailbox WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$mailboxresult = $sql->fetch();
$sql = $mail_db->prepare("SELECT * FROM alias WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
$aliasresult = $sql->fetch();
if (!$mailboxresult && !$aliasresult) {
$sql = $mail_db->prepare("DELETE FROM domain WHERE domain=:domain");
$sql->bindParam(':domain', $domaincheck[1]);
$sql->execute();
}
As you can see, it's pretty repetitive but there's a subtle difference for each module in the code, so don't paste the same code into the four files, paste the correct snippet from above into each of the four files and see how it goes for you.
You might also want to do a:
Code:
cp /etc/sentora/panel/modules/xxxxx/code/postfix.php /etc/sentora/panel/modules/xxxxx/code/postfix.php.old
for each file before you make the changes then you will have a backup copy of the original files if the world ends due to the changed code...
Keith