Here is a shell script that will iterate through all your Plesk domains to quickly add email addresses in bulk without going through each domain’s settings yourself.

  1. Intro
  2. Script
  3. Automation with events
  4. Related stuff

Intro

This specific script adds a webmaster email address with standard aliases like postmaster@ and abuse@, but without an actual mailbox. The messages are forwarded to an address at another domain name. It will also enable the antivirus scanning.

When an address already exists it writes an error and continues to the next domain.

Script

You can run the code manually to process all of your existing domains, but with the instructions below you can also have it executed each time a new domain is added to Plesk Onyx.

#!/bin/bash

if [ $NEW_DOMAIN_NAME ]; then
  domains=$NEW_DOMAIN_NAME
else
  query="SELECT name FROM domains WHERE parentDomainId=0;"
  domains=$(plesk db -Ne "$query")
fi

count=$(echo $domains | wc -w)
i=0

echo -e "\e[33mAdding webmaster emails to \e[1m$count\e[0m\e[33m domains\e[0m"
echo

for domain in $domains; do
  i=$(expr $i + 1)
  echo -n "$i/$count - "

  plesk bin mail \
    --create webmaster@$domain \
    -mailbox false \
    -aliases add:abuse,hostmaster,postmaster,mail,mailer-daemon,sysadmin \
    -forwarding true \
    -forwarding-addresses add:info@otherdomain.tld \
    -antivirus inout \
    -cp-access false
done

Make sure to change info@otherdomain.tld to your real destination.

Automation with events

Save it in /root/bin/domains_add_webmaster.sh and make it executable only by the root user:

chmod 0700 /root/bin/domains_add_webmaster.sh

Now go to Tools & Settings > Event Manager > Add Event Handler and enter the following details:

Event:
Domain created

Priority:
Lowest (0)

User:
root

Command:
/root/bin/domains_add_webmaster.sh

 

Categories: Code

Leave a Reply

Your email address will not be published. Required fields are marked *