Quick email forwarder in Shell

I’m always create new forwarding emails for different websites – helps me track down if and when their email list is hacked… To save logging into cPanel or WHM, I’ve created a little script to quickly add new forwarding emails.

#!/bin/sh

##############################################################
#                                                            #
#  QUICK EMAIL FORWARDER                                     #
#                                                            #
#  Coded by Noah Hearle, Design Extreme                      #
#  https://designextreme.com                                 #
#                                                            #
#  Created: 2017/01/23                                       #
#  Modified: 2019/06/11                                      #
#                                                            #
#  Post your comments at:                                    #
#  https://blog.nahoo.co.uk/quick-email-forwarder/       #
#                                                            #
#  Usage: sh ./quick_email_forwarder.sh <alias> [--delete]   #
#                                                            #
##############################################################

## ARGUMENTS ##

alias=$1
delete=$2

## SETTINGS ##

user=abcdomain
domain=abcdomain.com
list=/etc/valiases/$domain
delete_regex='^[-]*d(elete)?$'
email=$alias@$domain
email_account=account@$domain

if [ ! -f "$list" ]; then
        echo -e "\e[38;5;202mError:\e[0m Domain name '$domain' doesn’t exist in /etc/valiases/"
        exit
fi

last_line="$(tail -n 1 $list)"

if [ -n "$2" ] && [[ $delete =~ $delete_regex ]]; then
        if [ "$(grep -c $email $list)" -eq 0 ]; then
                echo -e "\e[33mWarning:\e[0m Cannot remove $email as it is not found"
                exit
        fi

        sed -i "/$email: $email_account/d" $list
        echo -e "\e[92mSuccess:\e[0m Removed email: $email"
        exit
fi

if [ "$(grep -c $email $list)" -gt 0 ]; then
        echo -e "\e[33mWarning:\e[0m $email already exists"
        exit
fi

sed -i '$ d' $list
echo $email: $email_account >> $list
sort $list -o $list
echo $last_line >> $list
chown --from=root.root $user.mail $list
echo -e "\e[92mSuccess:\e[0m Added email: $email"

Edit the settings to fit your domain and email setup. For multiple destinations, just use “, ” to separate the email addresses.

To make your special new email address, simply call the script with the alias as the only argument with an option -d or –delete command to remove an existing forwarder.

sh ./quick_email_forwarder.sh alias [--delete]

Please let me know what you think!

Last updated on

3 thoughts on “Quick email forwarder in Shell”

  1. Just wanted to say how much I appreciated this blog post. It’s rare to come across content that actually feels useful without sounding too robotic or salesy. The way you explained things was simple yet effective, and I could genuinely relate to the examples you used. It’s clear that you’ve put time and thought into this, and it really shows. I’ve read a lot of blogs on this topic, but most of them just repeat the same things without adding any real value—this one felt different. It actually gave me some new ideas and a better understanding. I’ve bookmarked it for future reference and will definitely be checking out more of your posts. Keep up the great work, and thanks again for sharing this!

Leave a Reply

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