Route emails locally for the same domain

tanmalik786

Registered
Feb 20, 2020
4
1
1
Lahore, PK
cPanel Access Level
Root Administrator
Hi there,
I have found that even my local emails are being sent out of my server and then are routed back to this domain, how can I route emails locally for my domain [example.com] to reside on the same server and the attachments if any are not duplicated either in this case are rather shared through "symbolic links".

If someone can share a Script for WHM to route emails locally for the same domain, that will be great. I did try this script but failed on two accounts
  1. it failed to fetch 'dig' command failed to fetch any MX record [even though when executed directly, it succeeds]
  2. I am using CentOS v7.9.2009 STANDARD cPanel v110.0.2, and I don't find any `postfix` folder under `/etc` or even `main.cf` file for that matter.
`
#!/bin/bash
# Add your domain name below​
domain="example.com"​
# Retrieve the local mail exchanger (MX) records for the domain​
mx_records=$(dig +short MX $domain | awk '{print $2}' | sort -n)​
# Create an array to store the local mail exchangers​
local_mx=()​
# Loop through each MX record and check if it points to the local server​
for mx in $mx_records​
do​
if [[ $mx == "$(hostname -f)" ]]; then​
local_mx+=($mx)​
fi​
done​
# Check if there are any local mail exchangers​
if [ ${#local_mx[@]} -eq 0 ]; then​
echo "No local mail exchangers found for $domain."​
exit 1​
fi​
# Create a Postfix transport map file​
transport_file="/etc/postfix/transport"​
# Create or overwrite the transport map file​
echo "$domain local:${local_mx[0]}" > $transport_file​
# Update the transport map​
postmap $transport_file​
# Configure Postfix to use Maildir format and create symbolic links for attachments​
main_cf="/etc/postfix/main.cf"​
attachments_dir="/var/mail/attachments"​
# Add or update the configuration in main.cf​
echo "​
# Configure Postfix to use Maildir format and symbolic links for attachments​
home_mailbox = Maildir/​
maildir_copy = no​
mime_directory = $attachments_dir​
" >> $main_cf​
# Create the attachments directory​
mkdir -p $attachments_dir​
# Set permissions for the attachments directory​
chown -R postfix:postfix $attachments_dir​
# Restart the Postfix service​
service postfix restart​
echo "Email routing for $domain has been configured to route locally."​
echo "Attachments will be shared using symbolic links."​
`
I will appreciate any assistance to get this resolved.
Thanks and Regards,
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,651
2,636
363
cPanel Access Level
Root Administrator
Hey there! cPanel doesn't use Postfix, so the code you provided wouldn't be helpful on a cPanel machine.

The setting you likely need to adjust is inside cPanel >> Email Routing for the affected domain. Just ensure that is set to the "Local mail exchanger" option and then system will deliver the messages locally.
 

tanmalik786

Registered
Feb 20, 2020
4
1
1
Lahore, PK
cPanel Access Level
Root Administrator
Thanks for your reply @cPRex, yes that's what I have concluded as well. I have set cPanel >> Email Routing to "Automatic", hope that's equally good?

I was wondering if there's a way to "not to duplicate" the attachments when they are sent to an email address of the same `domain`, and instead the same `attachment` is shared through a Symbolic Link.

Keenly awaiting for your response?
Thanks again!
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,651
2,636
363
cPanel Access Level
Root Administrator
Automatic should be just fine as well.

Are you saying that if one email with an attachment is sent to [email protected] and also [email protected] that you'd like the attachment to only physically exist in one spot on the server and the others are a link to it? I'm assuming the goal is to save disk space, but I don't believe that is possible in a standard email environment because each email user is limited to their own directory structure. There isn't a "public" area like you could create with FTP.
 

tanmalik786

Registered
Feb 20, 2020
4
1
1
Lahore, PK
cPanel Access Level
Root Administrator
Automatic should be just fine as well.

Are you saying that if one email with an attachment is sent to [email protected] and also [email protected] that you'd like the attachment to only physically exist in one spot on the server and the others are a link to it? I'm assuming the goal is to save disk space, but I don't believe that is possible in a standard email environment because each email user is limited to their own directory structure. There isn't a "public" area like you could create with FTP.
I wouldn't mind this a bit either, what actually I was asking was that "if [email protected] sends an email to [email protected] and [email protected] and it does contain an attachment as well [email is being sent and received on the same domain]" that attachment should be shared amongst the users through a "Symbolic Link"?