Array of usernames
usernames=(u_ootpuk u_collections_uk _kscgm-007_mail_test)

Array of passwords

passwords=(xxx yyy zzz)

Array of TO-List e-mails

tolist=(
<email>
)

POP3 server details

pop3_server="<server name>”
pop3_port="<port>"

Function to count email messages

count_messages() {
username=$1
password=$2

Connect to POP3 server using Telnet

    output=$(echo -e "USER $username\\nPASS $password\\nSTAT\\nQUIT" | ncat $pop3_server $pop3_port 2>/dev/null)

Extract the number of email messages

    count=$(echo "$output" | grep -oP '^\\+OK [0-9]+' | tr -d ' ')
    messages=$(echo $count | tr -d ' ' | cut -c 4)

Check if the number of messages is greater than 5

if [[ $messages -gt 25 ]]
then
# Send an alert via non-secure SMTP
# Iterate over e-mail IDs in tolist array
for ((j=0; j<${#tolist[@]}; j++)); do
# SMTP server details
SMTP_SERVER="<server name>"
SMTP_PORT="25"
# Email details
TO="${tolist[$j]}"
TOLIST="<email>"
FROM="<email>"
SUBJECT="[POP3-ALERT] High Message Count Alert"
MESSAGE="The user '"$username"' has $messages messages in pop3 account inbox(KSCGM-VIP4). Kindly take necessary action and restart CIR comp if required."

			echo "$username -- $messages";
			
    done

fi

}

Iterate over usernames and passwords

for ((i=0; i<${#usernames[@]}; i++)); do
count_messages "${usernames[$i]}" "${passwords[$i]}"
done