Linux Web Hosting, DevOps, and Cloud Solutions

Adding Domain Aliases in iRedMail: A Simple bash script

iRedMail is a powerful and open-source mail server solution that simplifies the process of setting up and managing email services. It supports popular email protocols, including IMAP, POP3, and SMTP, and can be used to host multiple email domains. In this guide, we’ll explore how to add domain aliases to iRedMail’s free version with a MySQL backend.

What Are Domain Aliases?
Domain aliases are additional domain names that point to an existing email domain. For example, if you have a primary domain like example.com, you can set up domain aliases like domain.ltd so that emails sent to username@domain.ltd are delivered to the corresponding mailbox of username@example.com. Domain aliases are a convenient way to manage multiple email addresses under a single domain.

The Bash Script:
Here’s a Bash script that simplifies the process of adding domain aliases in iRedMail. You can use this script to automate the task:

#!/bin/bash

# Author: 	Abdul Wahab
# Website: 	Linuxwebhostingsupport.in
# Print purpose and note
printf "Purpose: Add an alias domain in iRedMail. \n\n"
printf "Note: Let's say you have a mail domain example.com hosted on your iRedMail server, if you add domain name domain.ltd as an alias domain of example.com, all emails sent to username@domain.ltd will be delivered to user username@example.com's mailbox. So here domain.ltd is the alias domain and example.com is the traget domain \n\n"

# Prompt the user to enter the alias domain name
read -p "Enter the alias domain name: " ALIAS_DOMAIN

# Prompt the user to enter the target domain name
read -p "Enter the target domain name: " TARGET_DOMAIN

# Connect to the vmail database and check if the target domain exists in the domain table
RESULT=`mysql vmail -N -B -e "SELECT COUNT(*) FROM domain WHERE domain='$TARGET_DOMAIN'"`
if [ $RESULT -ne 1 ]
then
  echo "Error: The target domain $TARGET_DOMAIN does not exist in the domain table. You need to add the target domain first"
  exit 1
fi

# Insert the alias domain record
mysql vmail <<EOF
INSERT INTO alias_domain (alias_domain, target_domain)
VALUES ('$ALIAS_DOMAIN', '$TARGET_DOMAIN');
EOF

# Print completion message
echo "Alias domain $ALIAS_DOMAIN has been added for $TARGET_DOMAIN."

How to Use the Script:

Copy the provided Bash script into a text file, e.g., add_domain_alias.sh.
Make the script executable by running the following command:

chmod +x add_domain_alias.sh

Execute the script by running ./add_domain_alias.sh in your terminal.
Follow the prompts to enter the alias domain and target domain names.
The script will connect to the MySQL database and insert the alias domain record.

Conclusion:
Adding domain aliases in iRedMail is a straightforward process, and the provided Bash script can simplify it even further. With domain aliases, you can efficiently manage multiple email addresses under a single domain, enhancing your email hosting capabilities.

Feel free to use this script to streamline your iRedMail email domain management, making it easier to accommodate various email addresses and domains.

Exit mobile version