Email Server Management Script

I set up a mail server using this guide. After creating the mail server, and setting it up to track information via a MariaDB database, I decided to write a script to let me manage the email information.

This script lets me:

  • Manage Domains
  • Manage Email Forwarders
  • Manage Email Accounts

The script is available from My Github Account

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#! /usr/bin/perl
################################################################################
#
#    Email Manager
#    
#    This program manages a email system database.  It is designed to manage 
#    an email server set up using the following tutorial:
#
#    https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-
#    mariadb-on-centos-7
#
#    Copyright (C) Jordan McGilvray
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

use strict;
use warnings;
use 5.010;
use DBI;
use Term::ANSIColor;
use Term::ReadKey;

#Waits for a key press
#Input: Any Key
#Output: Displays the main menu
#Returns: None
#DB Queries: None
sub waitForIt
{
        print (colored( "\nPress Any Key to Continue\n", 'green'));
        ReadMode('cbreak');
        my $waitForIt = ReadKey(0);
        ReadMode('normal');
        &mainMenu();
}

#Database Connection Info
#Input: Contents of the .connect file
#Returns: Array of Database Connection Info
#Output: Possible Error if the .connect file cannot be opened
#The .connect file needs to be 4 lines long. It has 1 value on each line.
#database name
#database user
#password
sub dbInfo
{
	open FILE, ".connect" or die $!;
	my @dbConnect = <FILE>;
	close(FILE);
	chomp @dbConnect;
	return @dbConnect;
}

#getDBtable gets the values in a database table and returns them
#Input: Variables: Database Name, Database User, Database User Password
#Returns: Array of Arrays containing the information
#Output: None
sub getDBtable
{
	#Declare Variables
	my @dbConnect = @_;
	my @tableContent;
	my $arrayX = 0;
	my $arrayY = 0;
	#Database Connection
	my $dbh = DBI->connect("DBI:mysql:$dbConnect[0]", "$dbConnect[1]", "$dbConnect[2]"                                                                                                                                         ) || die "Could Not Connect to Database: $DBI::errstr";
	#Define the query to get the contents of the Database
	my $sql = qq{ SELECT * FROM $dbConnect[3] };
	#Prepare and Execute the query
	my $sth = $dbh->prepare( $sql );
	$sth->execute();
	#Get the Table Headers
	my $columnHeaders = $sth->{NAME};
	#Place the contents into the Array of Arrays
	while (my @row = $sth->fetchrow_array()) 
	{
		my $rowLength = scalar(@row);
		$arrayY = 0;
		foreach (@row)
		{
			$tableContent[$arrayX][$arrayY] = $_;
			$arrayY++;
		}
		$arrayX++;
	}
	#Finish with the query
	$sth->finish();
	#Disconnect from the database
	$dbh->disconnect();
	#Add Headers to the return array
	unshift @tableContent, $columnHeaders;
	#Return Table Content
	return @tableContent;
}

#Formats and Displays the content of a Database Table
#Input: Array of Arrays Containing the table
#Returns: None
#Output: Formatted Database Table
sub displayDBtable
{
	system("clear");
	#Get the Database Table Content
	my @tableContent = @_;
	#Get the Table Name and Capitalize it
	my $TableName = uc(shift(@tableContent));
	#Display the formatted Table
	print (colored( sprintf("%-40s","$TableName"),'yellow' ),"\n==========\n");
	my $arrayCounter = 0;
	foreach (@tableContent)
	{
		if ($arrayCounter == 0)
		{
			foreach (@$_)
			{
				my $text = ucfirst($_);
				print (colored( sprintf("%-40s","$text"),'cyan' ));
			}
		}
		else
		{
			foreach (@$_)
			{
				printf "%-40s","$_";
			}
		}
		$arrayCounter++;
		print "\n";
	}
	print "==========\n";
	#Varient of the waitForIt subroutine.  Returns to the Display menu
	print (colored( "\nPress Any Key to Continue\n", 'green'));
	ReadMode('cbreak');	
	my $waitForIt = ReadKey(0);
	ReadMode('normal');
	displayTable();
}

#Add a new entry to a Database Table
#I used given/when because there can only be 3 options via the menu.  I thought of making each of these its own subroutine, but I like keeping the functionality together. 
#Input: Values for the new entry
#Returns: None
#Output: None
#DB QUERIES:
#	Domains: INSERT INTO domains (domain) VALUES("$newDomain")
#	Email Account: INSERT INTO users (email, password) VALUES("$newEmailAddress", ENCRYPT("$newEmailPassword"))
#	Forwarders: INSERT INTO forwardings VALUES("$newEmailAddress", "$newForwardToAddress")
sub addToTable
{
	my @dbConnect = dbInfo();
	my $tableName = @_[0];
	my $newVal1='';
	my $newVal2='';
	my $sql;
	my @dbTable = getDBtable(@dbConnect, $tableName); 
	my $columnName = shift @dbTable;
	my $columnCount = scalar @$columnName;
	#Take in the first (or only) value for adding entries to the database
	print (colored("Enter the New ".ucfirst(@$columnName[0])." to Add.\n#", 'cyan'));
	chomp($newVal1 = <STDIN>);
	#If there is a second column (Password or actual email account, depending on if it is a forwarder or email account
	if ($columnCount == 2)
	{
		print (colored("Enter the New ".ucfirst(@$columnName[1])." to Add.\n#", 'cyan'));
		chomp($newVal2 = <STDIN>);
	}
	#Connect to the database
	my $dbh = DBI->connect("DBI:mysql:$dbConnect[0]", "$dbConnect[1]", "$dbConnect[2]"                                                                                                                                         ) || die "Could Not Connect to Database: $DBI::errstr\n";
	#Determine which query to run
	given ( $tableName )
	{
		when("domains")		
		{
                	$sql = qq{ INSERT INTO $tableName (@$columnName[0]) VALUES ("$newVal1") };
			break; 
		}
		when("users")
		{
                	$sql = qq{ INSERT INTO $tableName VALUES("$newVal1", ENCRYPT("$newVal2")) };
			break; 
		}
		when("forwardings")
		{ 
                	$sql = qq{ INSERT INTO $tableName VALUES("$newVal1", "$newVal2")  };
			break; 
		}
	}
	#Close Prepare, execute, and finish the query.  Then disconnect from the database
	my $sth = $dbh->prepare( $sql );
	$sth->execute();
	$sth->finish();
	$dbh->disconnect();
	#Wait for a keypress before continuing. 
	waitForIt();
}

#Delete an entry to a Database Table
#Input: Entry to delete
#Returns: None
#Output: None
#DB Query: DELETE FROM $tableName WHERE $columnName = "$databaseRows{$inputItem}" limit 1 
sub removeFromTable
{
	my $tableName = $_[0];
	#Get the Database Connection Information
	my @dbConnect = dbInfo(); 
	#Get the Database Table Content
	my @dbTable = getDBtable(@dbConnect,$tableName);
	my $rowCounter = 1;
	my %databaseRows;
	my $inputItem = 0;
	my $key;
	my $value;
	#Retrieve the main column name
	my $columnName = shift @dbTable;
	$columnName = "@$columnName[0]";
	#Create a menu of items that can be  deleted
	foreach (@dbTable)
	{
		$databaseRows{"$rowCounter"} = "@$_[0]";
		$rowCounter++;
	}
	#This is where the database is sorted.  It is not a numerical sort.  So 10 follows 1.
	foreach $key(sort keys %databaseRows)
	{
		print "$key. $databaseRows{$key}\n";
	}
	print "$rowCounter. Return to the Main Menu\n";
        print (colored("Which Entry Would You Like To Delete?\nType the number next to the entry.\n#", 'cyan'));
	#I limit input to the values of the menu
	while (($inputItem > $rowCounter) || ($inputItem < 1))
	{
		chomp($inputItem = <STDIN>);
	}
	#Exit back to main menu if a deletion is not needed.
	if ($inputItem eq $rowCounter)
	{
		&mainMenu ();
	}
	#Connect to the Database
        my $dbh = DBI->connect("DBI:mysql:$dbConnect[0]", "$dbConnect[1]", "$dbConnect[2]"                                                                                                                                         ) || die "Could Not Connect to Database: $DBI::errstr";
	#Create the query to delete the row
        my $sql = qq{ DELETE FROM $tableName WHERE $columnName = "$databaseRows{$inputItem}" limit 1 };
	#Prepare and execute the query
        my $sth = $dbh->prepare( $sql );
        $sth->execute();
        #Finish with the query
        $sth->finish();
        #Disconnect from the database
        $dbh->disconnect();
	#Wait for a keypress and return to the main menu
	waitForIt();
}

#Edits entry to a Database Table
#Input: entry to edit, and new values
#Returns: None
#Output: None
#DBQueries: 
sub editTable
{
	my @dbConnect = dbInfo();
	my $tableName = $_[0];
	my $rowCounter = 1;
	my $columnCount = 1;
	my $newCol1Value;
	my $newCol2Value;
	my $tableHeader;
	my $key=0;
	my $inputItem=0;
	my @newColVals;
	my @tableHeaders;
	my @dbTable = getDBtable(@dbConnect,$tableName);
	my %dbRows;
	#Get the Table Headers
	$tableHeader = shift @dbTable;
	foreach (@$tableHeader)
	{
		push (@tableHeaders,$_);
	}
	#Get the number of columns for the upcoming MySQL query
	$columnCount = scalar @tableHeaders;
	#Display Database Table
	foreach (@dbTable)
	{
                $dbRows{"$rowCounter"} = "@$_[0]";
		$rowCounter++;
	}
        #This is where the database is sorted.  It is not a numerical sort.
        foreach $key(sort keys %dbRows)
        {
        	print "$key. $dbRows{$key}\n";
        }
        print "$rowCounter. Return to the Main Menu\n";
        print (colored("Which Entry Would You Like To Edit?\nType the number next to the entry.\n#", 'cyan'));
        #I limit input to the values of the menu
        while (($inputItem > $rowCounter) || ($inputItem < 1))
        {
		print "\n$inputItem, $rowCounter, Stuck in the Input Selection Loop\n";
        	chomp($inputItem = <STDIN>);
        }
        #Exit back to main menu if a deletion is not needed.
        if ($inputItem eq $rowCounter)
        {
        	&mainMenu ();
        }
	#Database Query Selector
	#UPDATE $tableName SET $tableHeaders[0]=new-$newInput, $tableHeaders[1]=new-$newInput1 WHERE $tableHeaders[0]=$dbRows{$inputItem}
	print "$columnCount\n";
	for(my $i=0; $i < $columnCount; $i++)
	{
		print (colored("Please enter a value for $tableHeaders[$i]\n#",'cyan'));
		chomp ($newColVals[$i] = <STDIN>);
	}
        my $dbh = DBI->connect("DBI:mysql:$dbConnect[0]", "$dbConnect[1]", "$dbConnect[2]"                                                                                                                                         ) || die "Could Not Connect to Database: $DBI::errstr";
        #Create the query to delete the row
        my $sql;
        #Prepare and execute the query
	given ( $columnCount )
	{
		when(1)
		{
			$sql = qq{ UPDATE $tableName SET $tableHeaders[0]="$newColVals[0]" WHERE $tableHeaders[0]="$dbRows{$inputItem}" };
			print qq{ UPDATE $tableName SET $tableHeaders[0]="$newColVals[0]" WHERE $tableHeaders[0]="$dbRows{$inputItem}" };
			break;
		}
		when(2)
		{
			$sql = qq{ UPDATE $tableName SET $tableHeaders[0]="$newColVals[0]",$tableHeaders[1]="$newColVals[1]" WHERE $tableHeaders[0]="$dbRows{$inputItem}" };
			break;
		}
	}
	my $sth = $dbh->prepare( $sql );
	$sth->execute();
	$sth->finish();
	$dbh->disconnect();
	waitForIt();
}

#Select Database Table to Display
#Input: None
#Returns: None
#Output: Display Tables Menu
sub displayTable
{
	system("clear");
        print (colored( "  _____  _           _               _______    _     _           \n", 'cyan'));
        print (colored( " |  __ \\(_)         | |             |__   __|  | |   | |          \n", 'cyan'));
        print (colored( " | |  | |_ ___ _ __ | | __ _ _   _     | | __ _| |__ | | ___  ___ \n", 'cyan'));
        print (colored( " | |  | | / __| '_ \\| |/ _` | | | |    | |/ _` | '_ \\| |/ _ \\/ __|\n", 'cyan'));
        print (colored( " | |__| | \\__ \\ |_) | | (_| | |_| |    | | (_| | |_) | |  __/\\__ \\\n", 'cyan'));
        print (colored( " |_____/|_|___/ .__/|_|\\__,_|\\__, |    |_|\\__,_|_.__/|_|\\___||___/\n", 'cyan'));
        print (colored( "              | |             __/ |                               \n", 'cyan'));
        print (colored( "              |_|            |___/                                \n", 'cyan'));
        print (colored( sprintf("    %-5s","1. View Domain Table"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","2. View Email Account Table"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","3. View Forwarder Table"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","4. Return to the Main Menu"),'yellow' ),"\n");
	my $choice = -1;
	my $warningCounter = 0;
	while (1)
	{
		print (colored( "Please Enter Your Selection\n", 'red'));
                print (colored("#", 'cyan'));
		while (($choice gt 4) || ($choice lt 1))
		{
			if ($warningCounter ne 0)
			{
				print "Please enter a valid choice.\n";
			}
			$warningCounter++;
                	chomp($choice = <STDIN>);
		}
                given ($choice)
                {
                	when(1)         { displayDBtable("Domains",getDBtable(dbInfo(),"domains")); break; } #View Domain Table
                        when(2)         { displayDBtable("Email Accounts",getDBtable(dbInfo(),"users")); break; } #View Email Account Table
                        when(3)         { displayDBtable("Forwarders",getDBtable(dbInfo(),"forwardings")); break; } #View Forwarder Table
			when(4)		{ &mainMenu(); break; } #Return to the Main Menu
                }
	}
}

#Display Domain Table Editing Menu
#Input: None
#Returns: None
#Output: Domain Editing Menu
sub editDomains
{
	system("clear");
        print (colored( "██████╗  ██████╗ ███╗   ███╗ █████╗ ██╗███╗   ██╗███████╗\n", 'cyan'));
        print (colored( "██╔══██╗██╔═══██╗████╗ ████║██╔══██╗██║████╗  ██║██╔════╝\n", 'cyan'));
        print (colored( "██║  ██║██║   ██║██╔████╔██║███████║██║██╔██╗ ██║███████╗\n", 'cyan'));
        print (colored( "██║  ██║██║   ██║██║╚██╔╝██║██╔══██║██║██║╚██╗██║╚════██║\n", 'cyan'));
        print (colored( "██████╔╝╚██████╔╝██║ ╚═╝ ██║██║  ██║██║██║ ╚████║███████║\n", 'cyan'));
        print (colored( "╚═════╝  ╚═════╝ ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝╚═╝  ╚═══╝╚══════╝\n", 'cyan'));
        print "Please Select the number of the option you wish\n";
        print (colored( sprintf("    %-5s","1. Add a Domain"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","2. Delete a Domain"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","3. Edit a Domain"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","4. Return to the Main Menu"),'yellow' ),"\n");
        my $choice = -1;
	my $warningCounter = 0;
        while (1)
        {
                print (colored( "Please Enter Your Selection\n", 'red'));
                print (colored("#", 'cyan'));
                while (($choice gt 4) || ($choice lt 1))
                {
                        if ($warningCounter ne 0)
                        {
                                print "Please enter a valid choice.\n";
                        }
			$warningCounter++;
                        chomp($choice = <STDIN>);
                }
                given ($choice)
                {
                	when(1)         { addToTable("domains"); break; } #Add a Domain
                        when(2)         { removeFromTable("domains"); break; } #Delete a Domain
                        when(3)         { editTable("domains"); break; } #Edit a Domain
                        when(4)         { mainMenu(); break; } #Return to the Main Menu
                }
        }
}

#Displays the Email editing menu
#Input: None
#Returns: None
#Output: Email Editing Menu
sub editEmail
{
	system("clear");
        print (colored( "▓█████  ███▄ ▄███▓ ▄▄▄       ██▓ ██▓    \n", 'cyan'));
        print (colored( "▓█   ▀ ▓██▒▀█▀ ██▒▒████▄    ▓██▒▓██▒    \n", 'cyan'));
        print (colored( "▒███   ▓██    ▓██░▒██  ▀█▄  ▒██▒▒██░    \n", 'cyan'));
        print (colored( "▒▓█  ▄ ▒██    ▒██ ░██▄▄▄▄██ ░██░▒██░    \n", 'cyan'));
        print (colored( "░▒████▒▒██▒   ░██▒ ▓█   ▓██▒░██░░██████▒\n", 'cyan'));
        print (colored( "░░ ▒░ ░░ ▒░   ░  ░ ▒▒   ▓▒█░░▓  ░ ▒░▓  ░\n", 'cyan'));
        print (colored( " ░ ░  ░░  ░      ░  ▒   ▒▒ ░ ▒ ░░ ░ ▒  ░\n", 'cyan'));
        print (colored( "   ░   ░      ░     ░   ▒    ▒ ░  ░ ░   \n", 'cyan'));
        print (colored( "   ░  ░       ░         ░  ░ ░      ░  ░\n", 'cyan'));
        print "Please Select the number of the option you wish\n";
        print (colored( sprintf("    %-5s","1. Add an Email Account"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","2. Delete an Email Account"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","3. Edit an Email Account"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","4. Return to the Main Menu"),'yellow' ),"\n");
        my $choice = -1;
	my $warningCounter = 0;
        while (1)
        {
                print (colored( "Please Enter Your Selection\n", 'red'));
                print (colored("#", 'cyan'));
                while (($choice gt 4) || ($choice lt 1))
                {
                        if ($warningCounter ne 0)
                        {
                                print "Please enter a valid choice.\n";
                        }
			$warningCounter++;
                        chomp($choice = <STDIN>);
                }
                given ($choice)
                {
                	when(1)         { addToTable("users"); break; } #Add a Domain
                        when(2)         { removeFromTable("users");  break; } #Delete a Domain
                        when(3)         { editTable("users"); break; } #Edit a Domain
                        when(4)         { mainMenu(); break; } #Return to the Main Menu
		}
        }
}

#Displays the forwarders editing menu
#Input: None
#Returns: None
#Output: Forwarders Editing Menu
sub editForwarders
{
	system("clear");
        print (colored( "____ ____ ____ _ _ _ ____ ____ ___  ____ ____ ____ \n", 'cyan'));
        print (colored( "|___ |  | |__/ | | | |__| |__/ |  \\ |___ |__/ [__  \n", 'cyan'));
        print (colored( "|    |__| |  \\ |_|_| |  | |  \\ |__/ |___ |  \\ ___] \n", 'cyan'));
        print "Please Select the number of the option you wish\n";
        print (colored( sprintf("    %-5s","1. Add a Forwarder"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","2. Delete a Forwarder"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","3. Edit a Forwarder"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","4. Return to the Main Menu"),'yellow' ),"\n");
        my $choice = -1;
	my $warningCounter = 0;
        while (1)
        {
                print (colored( "Please Enter Your Selection\n", 'red'));
                print (colored("#", 'cyan'));
		while (($choice gt 4) || ($choice lt 1))
		{
			if ($warningCounter > 0)
			{	
                                print "Please enter a valid choice.\n";
			}
			$warningCounter++;
			chomp($choice = <STDIN>);	
		}
                given ($choice)
                {
                	when(1)         { addToTable("forwardings"); break; } #Add a Domain
                        when(2)         { removeFromTable("forwardings"); break; } #Delete a Domain
                        when(3)         { editTable("forwardings"); break; } #Edit a Domain
                        when(4)         { mainMenu(); break; } #Return to the Main Menu
                }
        }
}

#Main Menu of program
#Input: None
#Returns: None
#Output: Main Menu
sub mainMenu
{
	system("clear");
        print (colored( "• ▌ ▄ ·.  ▄▄▄· ▪   ▐ ▄     • ▌ ▄ ·. ▄▄▄ . ▐ ▄ ▄• ▄▌\n", 'cyan'));
        print (colored( "·██ ▐███▪▐█ ▀█ ██ •█▌▐█    ·██ ▐███▪▀▄.▀·•█▌▐██▪██▌\n", 'cyan'));
        print (colored( "▐█ ▌▐▌▐█·▄█▀▀█ ▐█·▐█▐▐▌    ▐█ ▌▐▌▐█·▐▀▀▪▄▐█▐▐▌█▌▐█▌\n", 'cyan'));
        print (colored( "██ ██▌▐█▌▐█ ▪▐▌▐█▌██▐█▌    ██ ██▌▐█▌▐█▄▄▌██▐█▌▐█▄█▌\n", 'cyan'));
        print (colored( "▀▀  █▪▀▀▀ ▀  ▀ ▀▀▀▀▀ █▪    ▀▀  █▪▀▀▀ ▀▀▀ ▀▀ █▪ ▀▀▀ \n", 'cyan'));
	print "Please Select the number of the option you wish\n";
        print (colored( sprintf("    %-5s","1. View Tables"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","2. Add, Delete, & Edit Domains"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","3. Add, Delete, & Edit  Email Accounts"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","4. Add, Delete, & Edit  Forwarders"),'yellow' ),"\n");
        print (colored( sprintf("    %-5s","5. Quit"),'yellow' ),"\n");
	my $choice = -1;
	my $warningFlag = 0;
	while (1)
	{
		print (colored( "Please Enter Your Selection\n", 'red'));
		print (colored("#", 'cyan'));
		while (($choice gt 5) || ($choice lt 1))
		{
			if ($warningFlag >0)
			{
                                print "Please enter a valid choice.\n";
			}
			$warningFlag++;
			chomp($choice = <STDIN>);
		}
		given ($choice)
		{
			when(1)		{ displayTable(); break; } #View Tables
			when(2)		{ editDomains(); break; } #Add and Delete Domains
			when(3)		{ editEmail(); break; } #Add and Delete Email Accounts
			when(4)		{ editForwarders(); break; } #Add and Delete Forwarders
			when(5)		{ print "Goodbye\n"; exit; } #Quit
		}
	}
}

mainMenu() || die;
Written on September 15, 2016