Skip to content

Instantly share code, notes, and snippets.

@MarcelFox
Last active April 11, 2018 00:25
Show Gist options
  • Save MarcelFox/5915018529220a467c3ac35d49b2b80c to your computer and use it in GitHub Desktop.
Save MarcelFox/5915018529220a467c3ac35d49b2b80c to your computer and use it in GitHub Desktop.
cp_testmail
#!/bin/bash
#
# Name: cp_testmail
# Version: 1.3.2
# By MarcelFox
#
# HOW TO WORK WITH?
# As 'root' and in a cPanel server run:
# bash <(curl -ks http://bashdump.cf/cp_testmail.sh)
#
# WHAT DOES?
# creates a predefined 'testmail' email account.
# If one exists, it gives us options to proceed.
#
# WHY ROCKS?
# with the '-n' argument, it creates a brand new
# email account! And provides password, links to
# direct access, and it's good for you my son!
#
#
### VARIABLES ###
cp_user=$(pwd | cut -d"/" -f3)
mail_user="testmail"
mail_domain=$(awk -v user="$cp_user" '$0 ~ user && gsub(":", "") {print $1}' /etc/trueuserdomains)
mail_pass=$(cat /dev/urandom|tr -dc "a-zA-Z0-9-_\$\?"|fold -w 10| head -n1)
var_aux="0"
#################
### FUNCTIONS ###
function create_mail() {
if [ $var_aux -eq "0" ];
then
addon_domains
fi
whmapi1 list_pops_for user=$cp_user | grep "$mail_user@$mail_domain" > /dev/null 2>&1
if [[ $? -eq 1 ]];
then
uapi --user=$cp_user Email add_pop email=$mail_user password=$mail_pass quota=250 domain=$mail_domain > /dev/null 2>&1
print_mailacct
else
printf "\nThe user '$mail_user@$mail_domain' already exists, create another email account? (y/n): "
read var_temp
if [[ $var_temp == "y" || $var_temp == "Y" ]];
then
new_account
elif [[ $var_temp == "n" || $var_temp == "N" ]];
then
change_pass
else
printf "\nInform only 'y' or 'n'\n"
fi
fi
}
function new_account() {
if [ $var_aux -eq "2" ];
then
addon_domains
fi
printf "\nInform the new username for the email account: "
read mail_user
var_aux="1"
create_mail
}
function change_pass() {
printf "\nChange password for the account '$mail_user@$mail_domain'? (y/n): "
read var_temp
if [[ $var_temp == "y" || $var_temp == "Y" ]];
then
uapi --user=$cp_user Email passwd_pop email=$mail_user password=$mail_pass domain=$mail_domain > /dev/null 2>&1
print_mailacct
elif [[ $var_temp == "n" || $var_temp == "N" ]];
then
printf "\nSkipping...\n"
else
printf "\nInform only 'y' or 'n'\n"
fi
exit
}
function addon_domains() {
var_temp=$(uapi --user=$(pwd | cut -d"/" -f3) DomainInfo list_domains | awk '$0 ~ "-" && !/*/ && NF>1 {print$2}')
if [ ! -z $var_temp ];
then
printf "\nThe user $cp_user has the following domains:\n\n"
printf "$(uapi --user=$(pwd | cut -d"/" -f3) DomainInfo list_domains | awk '$0 ~ "-" && !/*/ && NF>1 {print" "$2}')\n"
printf "\nDo you want to create an email with one of these domains, instead '$mail_domain'? (y/n): "
read var_temp
if [[ $var_temp == "y" || $var_temp == "Y" ]];
then
printf "\nPlease inform the domain to create the new mail account: "
read addon_domain
check_domain
elif [[ $var_temp == "n" || $var_temp == "N" ]];
then
printf "Skipping...\n"
else
printf "\nInform only 'y' or 'n'\n"
fi
fi
}
function check_domain() {
var_temp=$(uapi --user=$cp_user DomainInfo single_domain_data domain=$addon_domain | awk '$0 ~ "status" {print$2}')
while [ $var_temp -eq "0" ];
do
printf "\nPlease inform a valid domain: "
read addon_domain
check_domain
done
mail_domain=$addon_domain
}
function print_mailacct(){
printf "\n\nWebmail Link: https://$(hostname):2096/login?user=$mail_user@$mail_domain&pass=$mail_pass\n"
printf "\n\tMAIL: $mail_user@$mail_domain\\n"
printf "\tPASS: $mail_pass\n\n\n"
unset cp_user
unset mail_domain
unset mail_pass
}
if [ ! -z $1 ];
then
var=$1
case $var in
"-n"|"--new")
var_aux="2"
new_account
;;
"-l"|"--list")
printf "Email Accounts:\n"
whmapi1 list_pops_for user=$cp_user | awk '/@/ {print " "$2}'
echo
exit
;;
"-h"|"--help")
printf "USAGE: --testmail [OPTION]\nOptions:\n"
printf " -n, --new\t\tcreates new email account."
printf " -l, --list\t\tlist email accounts."
printf " -h, --help\t\tShow this message."
echo
exit
;;
*)
printf "\n\nSorry, use '--help' for more information.\n\n"
;;
esac
fi
create_mail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment