Sync Hestia new mail domain to Proxmox Mail Gateway as Relay Domain

After you successfully imported all Hestia mail domains into your Proxmox Mail Gateway, you might want to sync Hestia to PMG when you add OR delete a mail domain in Hestia as an allowed Relay Domain mail destination on your Proxmox Mail Gateway. Here’s the how-to!

Make sure your Hestia server can establish a trusted SSH connection to the PMG server, using private keys. You can find plenty of guides on how to do so.

On the Hestia server, create the hooks folder and the add domain script:

mkdir /usr/local/hestia/data/hooks/
cd /usr/local/hestia/data/hooks/
vi v-add-mail-domain-after

Enter:

#!/bin/bash
ssh pmg pmgsh create /config/domains --domain "$2" --comment "$1_`date "+%Y-%m-%d"`"

Save and exit vi. This will connect to your PMG server and create the mail domain with the username and creation date in the comment.

Create the delete domain script:

vi v-delete-mail-domain-after

Enter:

#!/bin/bash
ssh pmg pmgsh delete /config/domains/$1

Save and exit vi. This will execute the command and delete the mail domain on the PMG server.

Make the scripts executable:

chmod +x *

Now you need to modify the v-add-mail-domain and v-delete-mail-domain Hestia scripts to execute the hooks.

vi /usr/local/hestia/bin/v-add-mail-domain-after

At the end of the file, before the exit entry, add this:

# Adding new domain to Proxmox Proxy List
/usr/local/hestia/data/hooks/v-add-mail-domain-after $user $domain
$BIN/v-log-action "$user" "Info" "Mail" "Mail relay entry on Proxmox added (Domain: $domain)."

Save and exit vi. This will execute the previously created hook and adds an entry into the user’s log file.

Now for the v-delete-mail-domain command:

vi /usr/local/hestia/bin/v-delete-mail-domain-after

At the end of the file, before the exit entry, add this:

# Delete mail domain from Proxmox Proxy list
sh /usr/local/hestia/data/hooks/v-delete-mail-domain-after $domain
$BIN/v-log-action "$user" "Info" "Mail" "Mail relay entry on Proxmox deleted (Domain: $domain)."

Save and exit vi.

That’s it! Now add your new mail domain and it’ll show up on your Proxmox Mail Gateway. Or delete it 🙂

Export Hestia mail domains into the Relay Domains in Proxmox Mail Gateway 8

See my blogpost about using PMG with Hestia. This is a quick and dirty script to get all Hestia mail domain names for all Hestia users into the Mail Proxy Relay Domains list.

First, on Hestia, export all configured mail domains for all user:

for user in $(v-list-users | awk '{print $1}')
do 
 v-list-mail-domains $user | awk '{print $1}' | grep -v -e 'USER' -e 'DOMAIN' -e '------' -e 'Error:' >> relay_domains.txt
done

Get the relay_domains.txt file to your Proxmox master or node server and run the import:

while IFS= read -r domain 
do
 pmgsh create /config/domains --domain $domain
 echo "Added relay domain: $domain" 
done < relay_domains.txt

Done.

Now you can continue with setting up the mail domains settings as described here.

HestiaCP / Hestia – Batch change mail domain settings for use with Proxmox Mail Gateway

So I have a very cool (and quite full) Hestia server but I do not like the anti-spam capabilities and I wanted to have a reliable incoming and outgoing mail cluster before it. I therefor installed Proxmox Mail Gateway 8 on three nodes to do the filtering. But I have over 20 users and more than 100 mail domains to change.

SO! Batch-mode on.

What I wanted is to disable anti-spam, antivirus and reject spam settings. And also enable the smart relay to use the PMG cluster for outgoing mail.

After importing all mail domain names into Proxmox as ‘relay domains’ (trusted domains for which Proxmox can accept emails), I wrote this script to get all configured mail domains for a Hestia user and change the settings.

Just save it as mailchange.sh, do a chmod +x mailchange.sh and run it with a username:

./mailchange.sh hestia.username

The script:

#!/bin/bash
clear
# Change this to your PMG cluster host and the relay port 
relayhost="relay.domain.com"
relayport="26"
RED="\033[31m"
GREEN="\033[32m"
NORMAL="\033[0;39m"
if [ -z "$1" ]; then
  printf "$RED"
  echo "Error: The first argument must be a valid username."
  printf "$NORMAL"
  exit 1
else
  user="$1"
fi
# Getting all domains for username
v-list-mail-domains $1 | awk '{print $1}' | grep -v -e 'DOMAIN' -e '------' -e 'Error:' > $1_domains.txt
# Showing current mail domain settings
echo ""
printf "$GREEN"
echo Current mail domains and settings
echo ""
printf "$NORMAL"
v-list-mail-domains $1
# You can still get out of here!
echo ""
printf "$RED"
read -p "Ready to change the settings? (yes/no) " yn
case $yn in
yes ) printf "$GREEN";
echo OK, we will proceed;;
no ) echo exiting...;
  printf "$NORMAL";
exit;;
* ) echo invalid response;
exit 1;;
esac
printf "$NORMAL"
while IFS= read -r domain
do
    echo ""
    v-delete-mail-domain-antispam $1 $domain >/dev/null
    v-delete-mail-domain-antivirus $1 $domain >/dev/null
    v-delete-mail-domain-reject $1 $domain >/dev/null
    v-delete-mail-domain-smtp-relay $1 $domain >/dev/null
    v-add-mail-domain-smtp-relay $1 $domain $relayhost "" "" $relayport >/dev/null
    printf "$GREEN"
    echo "Removed settings and added smart relay for domain $domain"
    printf "$NORMAL"
done < $1_domains.txt
echo ""
printf "$GREEN"
echo New mail domains and settings
echo ""
printf "$NORMAL"
v-list-mail-domains $1

Before:

After:

Done!