This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Jump to the post that solved this thread.
SMTP Error every time i try to send an email
#7
[Solved] RE: SMTP Error every time i try to send an email
(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
Reply
Thanks given by: TGates , wormsunited
Jump to the post that solved this thread.


Messages In This Thread
RE: SMTP Error every time i try to send an email - by fearworks - 09-24-2018, 01:50 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
Email has suddenly stopped coming through rsthomas 3 855 06-20-2022, 10:51 PM
Last Post: fearworks
user: 'postfix' host: 'localhost' (Got an error reading communication packets) cezars 0 1 216 02-01-2022, 08:58 PM
Last Post: cezars
Why can i not send emails from my site phabeon8 3 3 652 04-20-2021, 06:29 AM
Last Post: sparkrack

Forum Jump:


Users browsing this thread: 1 Guest(s)