usmannasir / cyberpanel

Cyber Panel - The hosting control panel for OpenLiteSpeed
GNU General Public License v3.0
1.52k stars 589 forks source link

Postfix not delivering emails to external MX for locally hosted domain #706

Open chiareu opened 3 years ago

chiareu commented 3 years ago

Hi, it's an old problem that I hope someone more capable than me can solve. The mails are sent directly LOCALLY, without an MX query to see if for the respective locally hosted domain, the MX is external.

As an example, let's say we have domain1.tld hosted on our server but we want to use Gsuite as email service. For that we configure our MX entries to point to Google mail server.

Now if we want to send an email from the same server(other domain or web forms) to a mailbox for above domain (contact@domain1.tld), the server will fail to deliver the email because it searching for that mailbox locally, not checking the MX.

I tried to use Postfix Transport Map, but I still don't have a solution that works perfectly on a server with several hosted domains, each with its own needs.

References and tests https://forums.cyberpanel.net/discussion/4212/need-advice-on-custom-website-configuration-remote-email-server

https://forums.cyberpanel.net/discussion/5074/dovecot-not-sending-emails-externally-when-domain-is-hosted-on-cyberpanel?

qtwrk commented 3 years ago

something like this ? https://serverfault.com/questions/137591/postfix-disable-local-delivery

chiareu commented 3 years ago

Thanks for the suggestion, I'll check the link. However, Postfix should be configured from the beginning to check the MX of the hosted domains. At least one option on each domain to set as non-local email.

joer80 commented 2 years ago

I am having this issue also. Cyberpanel should have a remote mail exchanger setting like cpanel has.

joer80 commented 2 years ago

I did a workaround by adding a column to the e_domains database table called remote. Set default value to 0 and made my domain that has the external email service set to 1.

Then I changed the query in /etc/postfix/mysql-virtual_domains.cf to query = SELECT domain AS virtual FROM e_domains WHERE remote = 0 and domain='%s'

When this domain is no longer included as a virtual_mailbox_domain, it seems to deliver just fine.

So in the future to make a domain use a remote exchanger, I edit the e_domains table in cyberpanel database and set remote column to 1 for that domain.

adenisot commented 2 years ago

I did a workaround by adding a column to the e_domains database table called remote. Set default value to 0 and made my domain that has the external email service set to 1.

Then I changed the query in /etc/postfix/mysql-virtual_domains.cf to query = SELECT domain AS virtual FROM e_domains WHERE remote = 0 and domain='%s'

You are a genius, it works perfectly now !! how it is possible that cyberpanel didn't implement this before, it is very simple but so useful. thanks for your help, you saved me hours of search.

dankoz51 commented 2 years ago

The remote addition to the query allowed it to send but then broke receiving for me

I added a new sql query /etc/postfix/mysql-virtual_transport.cf

made the query = query = SELECT "smtp:[aspmx.l.google.com]:25" as transport FROM e_domains WHERE NOT EXISTS (SELECT * FROM e_users WHERE email = '%s') AND domain='%d'

then added this to main.cf transport_maps = proxy:mysql:/etc/postfix/mysql-virtual_transport.cf

basically if there is not user in the loca db it send it to google

joer80 commented 2 years ago

broke receiving

This fix is for if the MX is external so you shouldn't be receiving with cyberpanel in the first place.

So if something like your contact us page is not checking dns correctly and is sending email to the local server when it is supposed to be sent to an external email provider.

The fix also does not hard code a provider so it would work with office 365 even.

amaroarcast commented 6 months ago

I did a workaround by adding a column to the e_domains database table called remote. Set default value to 0 and made my domain that has the external email service set to 1.

Then I changed the query in /etc/postfix/mysql-virtual_domains.cf to query = SELECT domain AS virtual FROM e_domains WHERE remote = 0 and domain='%s'

When this domain is no longer included as a virtual_mailbox_domain, it seems to deliver just fine.

So in the future to make a domain use a remote exchanger, I edit the e_domains table in cyberpanel database and set remote column to 1 for that domain.

Thanks! I could not get it to not do localdelivery in any way, with this it worked.