-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailx
914 lines (738 loc) · 29.2 KB
/
mailx
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#!/bin/bash
# Function to check if a string exists in a file
check_string_in_file() {
local file=$1
local string=$2
grep -qF "$string" "$file" 2>/dev/null
}
# Function to generate a blowfish secret
generate_blowfish_secret() {
tr -dc 'a-zA-Z0-9./' < /dev/urandom | fold -w 32 | head -n 1
}
function hide_output {
OUTPUT=$(mktemp)
set +e
"$@" &> "$OUTPUT"
E=$?
set -e
if [ $E != 0 ]; then
# Something failed.
echo
echo "FAILED: $*"
echo -----------------------------------------
cat "$OUTPUT"
echo -----------------------------------------
exit $E
fi
rm -f "$OUTPUT"
}
# Function to update a setting in the php.ini file
update_php_ini() {
local setting=$1
local value=$2
local ini_file=$3
if grep -q "^${setting} = " "$ini_file"; then
sed -i "s/^${setting} = .*/${setting} = ${value}/" "$ini_file"
else
echo "${setting} = ${value}" >> "$ini_file"
fi
}
function apt_get_quiet {
DEBIAN_FRONTEND=noninteractive hide_output apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" "$@"
}
function apt_install {
apt_get_quiet install "$@"
}
# Function to retrieve stored password
retrieve_file_data() {
local filepath=$1
if [ -f "$filepath" ]; then
cat "$filepath"
else
echo "Error: Password file $filepath not found."
exit 1
fi
}
function restart_service {
hide_output service "$1" restart
}
# Function to generate a random password with only letters and numbers
generate_password() {
local length=$1
local chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
local password=""
for i in $(seq 1 $length); do
password+=$(echo $chars | fold -w1 | shuf | head -n1)
done
echo $password
}
# Function to configure Dovecot for quotas
configure_dovecot_quota() {
echo "Configuring Dovecot for quota support..."
# Backup existing configuration files
cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak
cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.bak
cp /etc/dovecot/conf.d/20-imap.conf /etc/dovecot/conf.d/20-imap.conf.bak
cp /etc/dovecot/conf.d/20-pop3.conf /etc/dovecot/conf.d/20-pop3.conf.bak
# Enable maildir format
# Define the new mail_location setting
new_mail_location="mbox:~/mail:INBOX=/var/mail/%u"
# Escape forward slashes for sed
escaped_mail_location=$(echo "$new_mail_location" | sed 's/\//\\\//g')
# Use sed to replace the mail_location line in the configuration file
sudo sed -i "s/^mail_location = .*$/mail_location = $escaped_mail_location/" /etc/dovecot/conf.d/10-mail.conf
# Enable quota support
cat <<EOT > /etc/dovecot/conf.d/90-quota.conf
plugin {
quota = maildir:User quota
quota_rule = *:storage=1G
}
protocol imap {
mail_plugins = \$mail_plugins quota
}
protocol pop3 {
mail_plugins = \$mail_plugins quota
}
EOT
echo "Dovecot quota configuration complete."
}
# Function to restart Dovecot service
restart_dovecot() {
echo "Restarting Dovecot service..."
systemctl restart dovecot
echo "Dovecot service restarted."
}
# Function to create or update the Postfix virtual alias file
update_postfix_virtual() {
local domain=$1
local username=$2
if ! check_string_in_file "/etc/postfix/virtual" "$username@$domain"; then
echo "$username@$domain $username" >> /etc/postfix/virtual
postmap /etc/postfix/virtual 2>/dev/null
# echo "Updated /etc/postfix/virtual with $username@$domain."
else
echo "Entry for $username@$domain already exists in /etc/postfix/virtual."
fi
}
# Function to update the Postfix main.cf configuration
update_postfix_maincf() {
local domain=$1
echo "Postfix main.cf configuration...."
if ! check_string_in_file "/etc/postfix/main.cf" "virtual_alias_domains"; then
echo "virtual_alias_domains = $domain" >> /etc/postfix/main.cf 2>/dev/null
else
sed -i "/^virtual_alias_domains/c\virtual_alias_domains = $domain" /etc/postfix/main.cf 2>/dev/null
fi
if ! check_string_in_file "/etc/postfix/main.cf" "virtual_alias_maps"; then
echo "virtual_alias_maps = hash:/etc/postfix/virtual" >> /etc/postfix/main.cf 2>/dev/null
fi
if ! check_string_in_file "/etc/postfix/main.cf" "mydomain"; then
echo "mydomain = $domain" >> /etc/postfix/main.cf 2>/dev/null
else
sed -i "/^mydomain/c\mydomain = $domain" /etc/postfix/main.cf 2>/dev/null
fi
if ! check_string_in_file "/etc/postfix/main.cf" "myorigin"; then
echo "myorigin = \$mydomain" >> /etc/postfix/main.cf 2>/dev/null
else
sed -i "/^myorigin/c\myorigin = \$mydomain" /etc/postfix/main.cf 2>/dev/null
fi
if ! check_string_in_file "/etc/postfix/main.cf" "mydestination"; then
echo "mydestination = \$myhostname, localhost.\$mydomain, localhost, \$mydomain" >> /etc/postfix/main.cf 2>/dev/null
else
sed -i "/^mydestination/c\mydestination = \$myhostname, localhost.\$mydomain, localhost, \$mydomain" /etc/postfix/main.cf 2>/dev/null
fi
echo "[OK]"
}
# Function to update Postfix configuration files
update_postfix_config() {
local domain=$1
local hostname=$2
echo "Downloading customized Postfix configuration files....."
# Backup original configuration files
cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
cp /etc/postfix/master.cf /etc/postfix/master.cf.bak
# Download customized Postfix configuration files
wget -qO /etc/postfix/main.cf https://raw.githubusercontent.com/gtmylab/mailx/main/postfix_main.cf
wget -qO /etc/postfix/master.cf https://raw.githubusercontent.com/gtmylab/mailx/main/postfix_master.cf
echo "[OK]"
echo "Replacing Postfix placeholders with actual values..."
# Replace placeholders with actual values in Postfix configuration files
sed -i "s/{EMAIL_DOMAIN}/$domain/g" /etc/postfix/main.cf 2>/dev/null
sed -i "s/{EMAIL_DOMAIN}/$domain/g" /etc/postfix/master.cf 2>/dev/null
sed -i "s/{HOSTNAME}/$hostname/g" /etc/postfix/main.cf 2>/dev/null
sed -i "s/{HOSTNAME}/$hostname/g" /etc/postfix/master.cf 2>/dev/null
echo "[OK]"
echo "Postfix configuration updated..."
# Restart Postfix to apply changes
restart_service postfix
echo "[OK]"
}
# Function to install phpmyadmin
setup_phpmyadmin() {
echo "Installing phpMyAdmin..."
# Variables
PMA_VERSION="5.1.1"
PMA_DIR="/var/www/html/phpmyadmin"
PMA_URL="https://files.phpmyadmin.net/phpMyAdmin/${PMA_VERSION}/phpMyAdmin-${PMA_VERSION}-all-languages.tar.gz"
PMA_TAR="phpMyAdmin-${PMA_VERSION}-all-languages.tar.gz"
PMA_TAR_DIR="phpMyAdmin-${PMA_VERSION}-all-languages"
# Download and extract phpMyAdmin
wget -q $PMA_URL -O $PMA_TAR
tar -xzf $PMA_TAR
# Move to the desired directory
sudo mv $PMA_TAR_DIR $PMA_DIR
# Generate a new secret
#SECRET=$(openssl rand -base64 32)
SECRET=$(generate_blowfish_secret)
# Create or update the config file with the new secret
cp $PMA_DIR/config.sample.inc.php $PMA_DIR/config.inc.php
CONFIG_FILE="$PMA_DIR/config.inc.php"
#sed -i "s/\$cfg\['blowfish_secret'\].*/\$cfg\['blowfish_secret'\] = '$SECRET';/" $CONFIG_FILE
sed -i "s|\(\$cfg\['blowfish_secret'\]\).*|\1 = '$SECRET';|" $CONFIG_FILE
# Set the appropriate permissions
sudo chown -R www-data:www-data $PMA_DIR
sudo find $PMA_DIR -type d -exec chmod 755 {} \;
sudo find $PMA_DIR -type f -exec chmod 644 {} \;
# Restart Apache to apply changes
sudo systemctl restart apache2
}
# Function to generate DKIM key and set global variables
generate_dkim() {
# Retrieve domain
DOMAIN=$(retrieve_file_data /usr/local/roundcube_mail_domain.txt)
# Ensure the domain is not empty
if [[ -z "$DOMAIN" ]]; then
echo "Error: No domain found in /usr/local/roundcube_mail_domain.txt"
exit 1
fi
# DKIM settings
SELECTOR="default"
DKIM_DIR="/etc/opendkim"
KEY_FILE="$DKIM_DIR/keys/$DOMAIN/$SELECTOR.private"
TXT_RECORD_FILE="$DKIM_DIR/keys/$DOMAIN/$SELECTOR.txt"
# Create necessary directories
sudo mkdir -p "$DKIM_DIR/keys/$DOMAIN"
# Generate DKIM key
sudo opendkim-genkey -s $SELECTOR -d $DOMAIN -D "$DKIM_DIR/keys/$DOMAIN" -b 2048
# Set permissions
sudo chown opendkim:opendkim "$KEY_FILE"
sudo chmod 600 "$KEY_FILE"
# Output the DNS TXT record
TXT_RECORD=$(cat "$TXT_RECORD_FILE")
# Clean up
sudo rm "$TXT_RECORD_FILE"
# Reload OpenDKIM
sudo systemctl restart opendkim
# Output key file location
echo "DKIM private key stored at: $KEY_FILE"
}
# Function to install Roundcube
install_roundcube() {
local EMAIL_DOMAIN=$1
local HOSTNAME=$2
local ADMIN_MAILX_EMAIL=$3
local MYSQL_ROOT_PASSWORD=$(retrieve_file_data /usr/local/mysql_root_pwd.txt)
local ROUNDCUBE_DB_PASSWORD=$(retrieve_file_data /usr/local/roundcube_db_pwd.txt)
local ROUNDCUBE_DB_USER="roundcube"
local ROUNDCUBE_DB_NAME="roundcubemail"
local ROUNDCUBE_ADMIN_EMAIL="admin@$EMAIL_DOMAIN"
local ROUNDCUBE_ADMIN_PASS=$(openssl rand -base64 12)
# Variables for the settings
POST_MAX_SIZE="50M"
UPLOAD_MAX_FILESIZE="50M"
MAX_EXECUTION_TIME="300"
MAX_INPUT_TIME="300"
MEMORY_LIMIT="256M"
# Path to the PHP-FPM ini file
PHP_INI_FILE="/etc/php/5.6/fpm/php.ini"
# Update hostname and hosts file
echo "$HOSTNAME" > /etc/hostname
sed -i "s/127.0.0.1.*/127.0.0.1 localhost $HOSTNAME/" /etc/hosts
hostnamectl set-hostname $HOSTNAME
# Ensure /etc/mailname exists and update it if hostname is a valid FQDN
# Ensure /etc/mailname exists and update it if hostname is a valid FQDN
if [[ "$HOSTNAME" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "$HOSTNAME" | tee /etc/mailname > /dev/null
else
echo "Warning: '$HOSTNAME' is not a fully qualified domain name. /etc/mailname not changed."
fi
clear
# Update system and install necessary packages
echo "Installing packages needed for setup..."
echo "."
echo ".."
echo "..."
echo "....please wait...."
# apt-get -qq update > /dev/null && apt-get -qq upgrade -y > /dev/null
apt-get -qq update > /dev/null
echo "System updated and upgraded successfully....[OK]"
echo "[OK]"
echo "Installing Software Properties..."
apt_install software-properties-common
echo "[OK]"
echo "Adding PHP PPA repository..."
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php -y > /dev/null 2>&1
echo "[OK]"
# Update package list again after adding new repository
apt-get -qq update > /dev/null
# Install MySQL without interactive prompts
echo "Installing MySQL (Database server)....."
# Install MySQL without interactive prompts
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD"
apt-get -qq install -y mysql-server > /dev/null
# Create Roundcube database and user
#echo "MySQL (Database server) warnings [OK]....."
# Create or overwrite the .my.cnf file
cat <<EOF > ~/.my.cnf
[client]
user=root
password=$MYSQL_ROOT_PASSWORD
EOF
# Set the appropriate permissions
chmod 600 ~/.my.cnf
# Create the database if it doesn't exist
mysql -e "CREATE DATABASE IF NOT EXISTS $ROUNDCUBE_DB_NAME;" 2>/dev/null
# Create the user if it doesn't exist and grant privileges
mysql -e "CREATE USER IF NOT EXISTS '$ROUNDCUBE_DB_USER'@'localhost' IDENTIFIED BY '$ROUNDCUBE_DB_PASSWORD';" 2>/dev/null
mysql -e "GRANT ALL PRIVILEGES ON $ROUNDCUBE_DB_NAME.* TO '$ROUNDCUBE_DB_USER'@'localhost';" 2>/dev/null
mysql -e "FLUSH PRIVILEGES;" 2>/dev/null
echo "[OK]"
# Install required packages for Roundcube
echo "Installing Postfix & Dovecot (Mail server)..."
apt_install postfix mailutils dovecot-core dovecot-imapd dovecot-pop3d dovecot-mysql -y
echo "[OK]"
configure_dovecot_quota
restart_dovecot
# Install opendkim if not installed
if ! command -v opendkim-genkey &> /dev/null; then
echo "Installing OpenDKIM..."
apt_install opendkim opendkim-tools
echo "[OK]"
fi
echo "Installing Apache (web server)..."
echo "[OK]"
apt_install apache2 libapache2-mod-fcgid libapache2-mod-php8.1 libapache2-mod-fcgid
# Backup original configuration files
cp /etc/apache2/ports.conf /etc/apache2/ports.conf.bak
wget -qO /etc/apache2/ports.conf https://raw.githubusercontent.com/gtmylab/mailx/main/apache2/ports.conf
# Install PHP 8.1 and PHP 5.6 with FPM and required extensions
echo "Installing PHP 8.1 and PHP 5.6..."
apt_install php8.1 php8.1-fpm php8.1-cli php8.1-common php8.1-curl php8.1-mysql php8.1-xml php8.1-mbstring php8.1-zip php8.1-intl php8.1-pspell
apt_install php5.6 php5.6-fpm php5.6-cli php5.6-common php5.6-curl php5.6-mysql php5.6-xml php5.6-mbstring php5.6-zip php5.6-intl php5.6-pspell
# Enable required Apache modules
echo "Enabling Apache modules..."
a2enmod actions fcgid alias proxy_fcgi > /dev/null 2>&1
echo "[OK]"
# Check PHP-FPM service name for PHP 8.1
PHPFPM_SERVICE=$(systemctl list-units --full --all | grep php8.1-fpm | awk '{print $1}')
if [ -z "$PHPFPM_SERVICE" ]; then
echo "PHP 8.1 FPM service not found. Please check your PHP installation."
exit 1
fi
# Configure PHP-FPM for PHP 8.1
mkdir -p /etc/php/8.1/fpm/pool.d
mkdir -p /etc/php/5.6/fpm/pool.d
wget -qO /etc/php/8.1/fpm/pool.d/www.conf https://raw.githubusercontent.com/gtmylab/mailx/main/php8/www.conf
# Configure PHP-FPM for PHP 5.6
wget -qO /etc/php/5.6/fpm/pool.d/www.conf https://raw.githubusercontent.com/gtmylab/mailx/main/php5/www.conf
# Update the settings in the php.ini file
update_php_ini "post_max_size" "$POST_MAX_SIZE" "$PHP_INI_FILE"
update_php_ini "upload_max_filesize" "$UPLOAD_MAX_FILESIZE" "$PHP_INI_FILE"
update_php_ini "max_execution_time" "$MAX_EXECUTION_TIME" "$PHP_INI_FILE"
update_php_ini "max_input_time" "$MAX_INPUT_TIME" "$PHP_INI_FILE"
update_php_ini "memory_limit" "$MEMORY_LIMIT" "$PHP_INI_FILE"
sudo systemctl restart php8.1-fpm 2>/dev/null
sudo systemctl restart php5.6-fpm 2>/dev/null
echo "Installing additional Mailing Support..."
# Download and install
mkdir -p /var/www/html/mailx
wget -qO /var/www/html/mailx/index.php https://raw.githubusercontent.com/gtmylab/mailx/main/mailer.php
chown -R www-data:www-data /var/www/html/mailx
echo "[OK]"
# Configure Apache for Roundcube
cat <<EOF > /etc/apache2/sites-available/roundcube.conf
<VirtualHost *:8080>
ServerName $HOSTNAME
DocumentRoot /var/www/html/roundcube
<Directory /var/www/html/roundcube/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dir.c>
DirectoryIndex index.php
</IfModule>
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog \${APACHE_LOG_DIR}/roundcube_error.log
CustomLog \${APACHE_LOG_DIR}/roundcube_access.log combined
</VirtualHost>
EOF
# Configure Apache for Mailx
cat <<EOF > /etc/apache2/sites-available/mailx.conf
<VirtualHost *:8081>
ServerName $EMAIL_DOMAIN
DocumentRoot /var/www/html/mailx
<Directory /var/www/html/mailx/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dir.c>
DirectoryIndex index.php
</IfModule>
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog \${APACHE_LOG_DIR}/mailx_error.log
CustomLog \${APACHE_LOG_DIR}/mailx_access.log combined
</VirtualHost>
EOF
# Configure Apache for PhPmyadmin
cat <<EOF > /etc/apache2/sites-available/phpmyadmin.conf
<VirtualHost *:8082>
ServerName $HOSTNAME
DocumentRoot /var/www/html/phpmyadmin
<Directory /var/www/html/phpmyadmin/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dir.c>
DirectoryIndex index.php
</IfModule>
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog \${APACHE_LOG_DIR}/phpmyadmin_error.log
CustomLog \${APACHE_LOG_DIR}/phpmyadmin_access.log combined
</VirtualHost>
EOF
# Disable old PHP version and enable PHP 8.1
a2enmod php8.1 > /dev/null 2>&1
systemctl restart apache2
echo "[OK]"
# Enable the sites and restart Apache
echo "Enabling site roundcube..."
a2ensite roundcube.conf > /dev/null 2>&1
echo "[OK]"
echo "Enabling site mailx..."
a2ensite mailx.conf > /dev/null 2>&1
echo "[OK]"
echo "Enabling site phpmyadmin..."
a2ensite phpmyadmin.conf > /dev/null 2>&1
echo "[OK]"
# Download and install Phpmyadmin
setup_phpmyadmin
echo "[OK]"
systemctl restart apache2
# Download and install Roundcube
echo "Downloading and installing Roundcube..."
cd /var/www/html
wget -q https://github.com/roundcube/roundcubemail/releases/download/1.5.0/roundcubemail-1.5.0-complete.tar.gz
tar -xzf roundcubemail-1.5.0-complete.tar.gz
mv roundcubemail-1.5.0 roundcube
chown -R www-data:www-data /var/www/html/roundcube
cd roundcube
cp config/config.inc.php.sample config/config.inc.php
echo "[OK]"
echo "Configuring Roundcube..."
# Configure Roundcube
# Check if mail_domain exists and add if not
sed -i "s/\$config\['db_dsnw'\].*/\$config\['db_dsnw'\] = 'mysql:\/\/$ROUNDCUBE_DB_USER:$ROUNDCUBE_DB_PASSWORD@localhost\/$ROUNDCUBE_DB_NAME';/" config/config.inc.php
sed -i "s/\$config\['default_host'\].*/\$config\['default_host'\] = 'localhost';/" config/config.inc.php
sed -i "s/\$config\['smtp_server'\].*/\$config\['smtp_server'\] = 'localhost';/" config/config.inc.php
sed -i "s/\$config\['support_url'\].*/\$config\['support_url'\] = 'mailto:$ROUNDCUBE_ADMIN_EMAIL';/" config/config.inc.php
sed -i "s/\$config\['smtp_port'\].*/\$config\['smtp_port'\] = 25;/" config/config.inc.php
sed -i "s/\$config\['smtp_user'\].*/\$config\['smtp_user'\] = '';/" config/config.inc.php
sed -i "s/\$config\['smtp_pass'\].*/\$config\['smtp_pass'\] = '';/" config/config.inc.php
sed -i "/\$config\['smtp_server'\]/i \$config['mail_domain'] = '$EMAIL_DOMAIN';" config/config.inc.php
echo "[OK]"
echo "Initialize Roundcube database schema..."
# Initialize Roundcube database schema
sudo -u www-data ./bin/initdb.sh --dir=./SQL --create
echo "[OK]"
# Update Postfix configurations
update_postfix_config "$EMAIL_DOMAIN" "$HOSTNAME"
# Call the generate_dkim function
generate_dkim
# Add First User
add_roundcube_user "admin" "$ROUNDCUBE_ADMIN_PASS" 1000
# Send notification to admin email
send_notification_email "$ADMIN_MAILX_EMAIL" "http://$HOSTNAME:8080" "roundcube" "$ROUNDCUBE_ADMIN_PASS" "http://$EMAIL_DOMAIN:8081" "$TXT_RECORD" "$SELECTOR" "http://$HOSTNAME:8082"
# Output Roundcube admin details
echo "Roundcube admin email: $ROUNDCUBE_ADMIN_EMAIL"
echo "Roundcube admin password: $ROUNDCUBE_ADMIN_PASS"
echo ""
echo ""
echo "MailX Installation Successful !"
}
# Function to add a new Roundcube user
add_roundcube_user() {
#local ROUNDCUBE_DB_PASSWORD=$1
local ROUNDCUBE_DB_USER="roundcube"
local ROUNDCUBE_DB_NAME="roundcubemail"
local username=$1
local domain=$(retrieve_file_data /usr/local/roundcube_mail_domain.txt)
local password=$2
local quota_size=$3 # Quota size in MB
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Combine username and domain to create full email address
email="$username@$domain"
# Generate encrypted password for Linux user
encrypted_password=$(openssl passwd -1 "$password")
# Check if user already exists
if id "$username" >/dev/null 2>&1; then
echo "User $username already exists. Aborting."
exit 1
fi
# Create user account
useradd -m -s /bin/bash -p "$encrypted_password" "$username"
echo ""
echo "User $username created successfully."
# Create Maildir for user
mkdir -p /home/$username/Maildir
chown -R $username:$username /home/$username/Maildir
echo ""
echo "Mail directory created for $username."
# Set up quota for the user
echo "quota_rule = *:storage=${quota_size}M" >> /home/$username/.dovecot.sieve
# Retrieve MySQL root and Roundcube DB passwords
MYSQL_ROOT_PASSWORD=$(retrieve_file_data /usr/local/mysql_root_pwd.txt)
ROUNDCUBE_DB_PASSWORD=$(retrieve_file_data /usr/local/roundcube_db_pwd.txt)
# Define the ROUNDCUBE_PREFERENCE variable
ROUNDCUBE_PREFERENCE='a:6:{s:14:"compose_extwin";i:1;s:10:"htmleditor";i:1;s:11:"mdn_default";b:0;s:11:"dsn_default";b:0;s:13:"sig_separator";b:0;s:25:"compose_save_localstorage";i:1;}'
# Insert new user and retrieve the auto-incremented ID
USER_ID=$(mysql $ROUNDCUBE_DB_NAME -se "
INSERT INTO users (username, mail_host, created, last_login, language, preferences) VALUES ('$username', 'localhost', NOW(), NOW(), 'en_GB', '$ROUNDCUBE_PREFERENCE');
SELECT LAST_INSERT_ID();
")
# Check if USER_ID was retrieved successfully
if [ -z "$USER_ID" ]; then
echo "Failed to retrieve USER_ID. Aborting."
exit 1
fi
# Insert into identities table
mysql $ROUNDCUBE_DB_NAME -e "
INSERT INTO identities (user_id, changed, del, standard, name, organization, email, \`reply-to\`, signature, html_signature)
VALUES ('$USER_ID', NOW(), '0', '1', '$username', 'localhost', '$email', '$email', '', '1');
"
# Update Postfix configurations
update_postfix_virtual "$domain" "$username"
update_postfix_maincf "$domain"
# Restart Postfix to apply changes
restart_service postfix
# Notify user via email
send_welcome_email "$email" "$username"
# Inform user creation details
echo "==============================================="
echo "New Roundcube User Details:"
echo "Email: $email"
echo "Username: $username"
echo "Password: $password"
echo "Quota: ${quota_size}M"
echo "==============================================="
}
# Function to uninstall Roundcube, including database and user
uninstall_roundcube() {
local MYSQL_ROOT_PASSWORD
MYSQL_ROOT_PASSWORD=$(retrieve_file_data /usr/local/mysql_root_pwd.txt)
# Drop Roundcube database and user
mysql -e "DROP DATABASE IF EXISTS roundcubemail;"
mysql -e "DROP USER 'roundcube'@'localhost';"
# Remove Roundcube directory
rm -rf /var/www/html/roundcube
echo "Roundcube uninstallation completed..."
}
# Function to uninstall Postfix, MySQL, and Roundcube
uninstall_all() {
echo "Uninstalling Postfix, MySQL, and Roundcube..."
# Call function to uninstall Roundcube database and user
uninstall_roundcube
# Stop services
systemctl stop postfix
systemctl stop apache2
systemctl stop mysql
# Remove packages
sudo apt-get -y remove --purge postfix apache2 php* roundcube mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* > /dev/null
sudo apt-get -y remove --purge mysql-server mysql-client mysql-common > /dev/null
# Remove configuration files
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql*
rm -rf /etc/postfix
rm -rf /etc/mysql
rm -rf /etc/apache2
rm -rf /etc/roundcube
rm -rf /var/www/html/roundcube
rm -rf /var/www/html/phpmyadmin
rm -rf /var/www/html/mailx
sudo deluser mysql
sudo delgroup mysql
echo '[OK]'
echo "Cleaning up system MailX System files..."
sudo apt-get -y clean > /dev/null
sudo apt-get -y autoremove > /dev/null
sudo apt-get -y autoclean > /dev/null
echo '[OK]'
echo "Uninstallation completed."
}
# Function to send email notification
send_notification_email() {
local recipient_email=$1
local roundcube_url=$2
local roundcube_db_user=$3
local roundcube_db_password=$4
local mailx_url=$5
local SELECTOR=$6
local TXT_RECORD=$7
local db_url=$8
local DOMAIN=$(retrieve_file_data /usr/local/roundcube_mail_domain.txt)
local subject="Roundcube Installation Successful"
local body="Dear User,
The Roundcube installation was successful. Here are the details:
Roundcube Database Username: $roundcube_db_user
Roundcube Database Password: $roundcube_db_password
Please keep these details secure.
You can now access:
Webmail URL via: $roundcube_url
Mailx URL via: $mailx_url
DB URL via: $db_url
Add the following TXT record to DNS settings:
Host: $SELECTOR._domainkey.$DOMAIN
Value: $TXT_RECORD
Best regards,
Your Server Team"
echo "$body" | mail -s "$subject" "$recipient_email"
}
# Function to send welcome email to user
send_welcome_email() {
local recipient_email=$1
local roundcube_user=$2
local subject="Welcome to Roundcube Webmail, $roundcube_user"
local body="Dear User,\n\nCongratulations!
Your Roundcube email has been successfully set up.
\n\nHere are your login details:\n\n
Email Address: $recipient_email\n
Username: $roundcube_user\n
You can access your Roundcube webmail interface anytime\n\n
Thank you,\nAdmin Team"
echo -e "$body" | mail -s "$subject" "$recipient_email"
}
# Function to send welcome email to user
send_test_message() {
# Install mailutils if not already installed
if ! dpkg -l | grep -q mailutils; then
apt-get update
apt-get install -y mailutils
fi
local email_to=$1
local email_subject=$2
local email_body=$3
local email_domain=$(retrieve_file_data /usr/local/roundcube_mail_domain.txt)
echo -e "$email_body" | mail -s "$email_subject" -a "From: admin@$email_domain" "$email_to"
echo "Test mail sent to $email_to"
}
clear
# Main script starts here
echo "=== Roundcube and Postfix Installation ==="
# Check if script is run with root privileges
if [ "$(id -u)" != "0" ]; then
echo "Error: This script must be run as root."
exit 1
fi
# Select an option
echo "Select an option:"
echo "1. Install MailX"
echo "2. Add MailX User"
echo "3. Send a test mail"
echo "4. Uninstall Roundcube"
echo "5. Uninstall MailX"
read -p "Enter your choice (1, 2, 3, 4 or 5): " choice
case $choice in
1)
clear
# Welcome message
echo "Welcome to the MailX Server Installation Script!"
echo
echo
# Loop until all required fields are entered correctly
while true; do
# Ask for user input
read -p "Please enter the email domain (e.g., example.com): " EMAIL_DOMAIN
read -p "Please enter the hostname (e.g., mail.example.com): " HOSTNAME
read -p "Please enter the admin email address: " ADMIN_MAILX_EMAIL
echo
# Validate inputs
if [[ -z "$EMAIL_DOMAIN" || -z "$HOSTNAME" || -z "$ADMIN_MAILX_EMAIL" ]]; then
echo "Error: All fields (Email Domain, Hostname, Admin Email) are mandatory. Please provide values for all."
continue
fi
# Validate hostname
if [[ ! "$HOSTNAME" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "Error: '$HOSTNAME' is not a fully qualified domain name. Please enter a valid FQDN."
continue
fi
# Display the entered information and ask for confirmation
echo "You have entered the following information:"
echo "Email Domain: $EMAIL_DOMAIN"
echo "Hostname: $HOSTNAME"
echo "Admin Email: $ADMIN_MAILX_EMAIL"
echo
read -p "Is this information correct? (y/n): " CONFIRM
if [ "$CONFIRM" = "y" ]; then
break
else
echo "Please re-enter the information."
fi
done
# Generate MySQL root password and Roundcube DB password
MYSQL_ROOT_PASSWORD=$(generate_password 16)
ROUNDCUBE_DB_PASSWORD=$(generate_password 16)
# Store passwords and domain in files (example)
echo "$MYSQL_ROOT_PASSWORD" > /usr/local/mysql_root_pwd.txt
echo "$ROUNDCUBE_DB_PASSWORD" > /usr/local/roundcube_db_pwd.txt
echo "$EMAIL_DOMAIN" > /usr/local/roundcube_mail_domain.txt
# Install Roundcube with specified configurations
install_roundcube "$EMAIL_DOMAIN" "$HOSTNAME" "$ADMIN_MAILX_EMAIL"
;;
2)
read -p "Enter new username: " username
# read -p "Enter domain: " domain
read -sp "Enter password for $username@$(retrieve_file_data /usr/local/roundcube_mail_domain.txt): " password
echo
# Call function to add Roundcube user
add_roundcube_user "$username" "$password" 2000
;;
3)
read -p "Email to: " email_to
read -p "Subject: " email_subject
read -p "Message Body: " email_body
# read -p "Enter domain: " domain
echo
# Call function to add Roundcube user
send_test_message "$email_to" "$email_subject" "$email_body"
;;
4)
read -p "Are you sure you want to uninstall ? (yes or no): " RECONFIRM_UNINSTALL
if [[ "$RECONFIRM_UNINSTALL" == "yes" ]]; then
uninstall_roundcube
else
echo "Exiting."
fi
exit 1
;;
5)
read -p "Are you sure you want to uninstall Postfix, MySQL, and Roundcube ? (yes or no): " RECONFIRM_UNINSTALL
if [[ "$RECONFIRM_UNINSTALL" == "yes" ]]; then
# Call function to uninstall Postfix, MySQL, and Roundcube
uninstall_all
else
echo "Exiting."
fi
exit 1
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac