Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Add and remove forwarding emails in shell

Discussion in 'Workarounds and Optimization' started by Nahoo, Jun 10, 2019.

  1. Nahoo

    Nahoo Member

    Joined:
    Oct 7, 2004
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    151
    Location:
    London, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    I've update an existing script to quickly add/remove forwarding emails for a particular domain

    Code:
    #!/bin/sh
    
    ##############################################################
    #                                                            #
    #  QUICK EMAIL FORWARDER                                     #
    #                                                            #
    #  Coded by Noah Hearle, Design Extreme                      #
    #  https://designextreme.com                                 #
    #                                                            #
    #  Created: 2017/01/23                                       #
    #  Modified: 2019/06/10                                      #
    #                                                            #
    #  Usage: sh ./quick_email_forwarder.sh <alias> [--delete]   #
    #                                                            #
    ##############################################################
    
    ## ARGUMENTS ##
    
    alias=$1
    delete=$2
    
    ## SETTINGS ##
    
    user=somedomain
    domain=somedomain.com
    list=/etc/valiases/$domain
    last_line="$(tail -n 1 $list)"
    delete_regex='^[-]*d(elete)?$'
    [email protected]$domain
    [email protected]$domain
    
    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"
    Please edit the settings in script before adding this to your script library. If you like it or would like me to extend it to any domain and account (as arguments), please let me know.
     
    #1 Nahoo, Jun 10, 2019
    Last edited: Jun 10, 2019
  2. cPanelLauren

    cPanelLauren Forums Analyst II Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    6,457
    Likes Received:
    503
    Trophy Points:
    263
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @Nahoo

    Thanks for sharing this with everyone!

    I do want to point out for anyone viewing this that while you're more than welcome to use custom 3rd party scripts/software we do not endorse the use of them and you do so at your own risk.


    Thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Nahoo

    Nahoo Member

    Joined:
    Oct 7, 2004
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    151
    Location:
    London, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    To make it a little more fool-proof, I've added an existence check for the domain...

    Code:
    #!/bin/sh
    
    ##############################################################
    #                                                            #
    #  QUICK EMAIL FORWARDER                                     #
    #                                                            #
    #  Coded by Noah Hearle, Design Extreme                      #
    #  https://designextreme.com                                 #
    #                                                            #
    #  Created: 2017/01/23                                       #
    #  Modified: 2019/06/11                                      #
    #                                                            #
    #  Usage: sh ./quick_email_forwarder.sh <alias> [--delete]   #
    #                                                            #
    ##############################################################
    
    ## ARGUMENTS ##
    
    alias=$1
    delete=$2
    
    ## SETTINGS ##
    
    user=somedomain
    domain=somedomain.com
    list=/etc/valiases/$domain
    delete_regex='^[-]*d(elete)?$'
    [email protected]$domain
    [email protected]$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"
     
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice