-
-
Notifications
You must be signed in to change notification settings - Fork 655
/
smtp-mail.sh
422 lines (360 loc) Β· 11.9 KB
/
smtp-mail.sh
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
#!/bin/bash
# T&M Hansson IT AB Β© - 2024, https://www.hanssonit.se/
# Copyright Β© 2021 Simon Lindner (https://github.com/szaimen)
true
SCRIPT_NAME="SMTP Relay with msmtp"
SCRIPT_EXPLAINER="This script will setup an SMTP Relay (Mail Server) in your Nextcloud Server \
that will be used to send emails about failed cronjob's and such."
# shellcheck source=lib.sh
source /var/scripts/fetch_lib.sh
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode
# Must be root
root_check
# Check if Smtp Relay was already configured
if ! [ -f /etc/msmtprc ]
then
# Ask for installing
install_popup "$SCRIPT_NAME"
else
# Ask for removal or reinstallation
reinstall_remove_menu "$SCRIPT_NAME"
# Removal
apt-get purge msmtp -y
apt-get purge msmtp-mta -y
apt-get purge mailutils -y
apt-get autoremove -y
rm -f /etc/mail.rc
rm -f /etc/msmtprc
rm -f /var/log/msmtp
echo "" > /etc/aliases
# Show successful uninstall if applicable
removal_popup "$SCRIPT_NAME"
fi
# Install needed tools
install_if_not msmtp
install_if_not msmtp-mta
install_if_not mailutils
# Default providers
choice=$(whiptail --title "$TITLE" --nocancel --menu \
"Please choose the mail provider that you want to use.
$MENU_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4 \
"mail.de" "(German mail provider)" \
"SMTP2GO" "(https://www.smtp2go.com)" \
"Manual" "(Complete manual setup)" 3>&1 1>&2 2>&3)
case "$choice" in
"mail.de")
NEEDS_CREDENTIALS=1
MAIL_SERVER="smtp.mail.de"
PROTOCOL="SSL"
SMTP_PORT="465"
;;
"SMTP2GO")
NEEDS_CREDENTIALS=1
SMTP2GO=1
MAIL_SERVER="mail-eu.smtp2go.com"
PROTOCOL="SSL"
SMTP_PORT="465"
;;
# Manual setup will be handled a few lines below
"")
msg_box "You haven't selected any option. Exiting!"
exit 1
;;
*)
;;
esac
print_text_in_color "$ICyan" "$choice was chosen..."
sleep 1
# Set everything up manually
if [ "$choice" = "Manual" ]
then
# Enter Mail Server
MAIL_SERVER=$(input_box_flow "Please enter the SMTP Relay URL that you want to use.\nE.g. smtp.mail.com")
# Enter if you want to use ssl
PROTOCOL=$(whiptail --title "$TITLE" --nocancel --menu \
"Please choose the encryption protocol for your SMTP Relay.
$MENU_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4 \
"SSL" "" \
"STARTTLS" "" \
"NO-ENCRYPTION" "" 3>&1 1>&2 2>&3)
if [ -z "$PROTOCOL" ]
then
exit 1
fi
case "$PROTOCOL" in
"SSL")
DEFAULT_PORT=465
;;
"STARTTLS")
DEFAULT_PORT=587
;;
"NO-ENCRYPTION")
DEFAULT_PORT=25
;;
*)
;;
esac
# Enter custom port or just use the default port
SMTP_PORT=$(whiptail --title "$TITLE" --nocancel --menu \
"Based on your selection of encryption the default port is $DEFAULT_PORT. Would you like to use that port or something else?
$MENU_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4 \
"Use default port" "($DEFAULT_PORT)" \
"Enter another port" "" 3>&1 1>&2 2>&3)
if [ -z "$SMTP_PORT" ]
then
exit 1
fi
case "$SMTP_PORT" in
"Use default port")
SMTP_PORT="$DEFAULT_PORT"
;;
"Enter another port")
SMTP_PORT="$(input_box_flow 'Please enter the port for your SMTP Relay.')"
;;
*)
;;
esac
fi
# Enter your SMTP username
if [ -n "$NEEDS_CREDENTIALS" ] || yesno_box_yes "Does $MAIL_SERVER require any credentials, like username and password?"
then
MAIL_USERNAME=$(input_box_flow "Please enter the SMTP username to your email provider.\nE.g. you@mail.com, or just the actual 'username'.")
# Enter your mail user password
MAIL_PASSWORD=$(input_box_flow "Please enter the SMTP password to your email provider.")
fi
# Enter the recipient
RECIPIENT=$(input_box_flow "Please enter the recipient email address that shall receive all mails.\nE.g. recipient@mail.com")
# Check if the server use self-signed certificates
if yesno_box_no "Does the SMTP-server use self-signed certificates?"
then
SELF_SIGNED_CERT=yes
nextcloud_occ config:system:set mail_smtpstreamoptions ssl allow_self_signed --value=true --type=boolean
nextcloud_occ config:system:set mail_smtpstreamoptions ssl verify_peer --value=false --type=boolean
nextcloud_occ config:system:set mail_smtpstreamoptions ssl verify_peer_name --value=false --type=boolean
else
SELF_SIGNED_CERT=no
fi
# Present what we gathered, if everything okay, write to files
msg_box "These are the settings that will be used. Please check that everything seems correct.
SMTP Relay URL=$MAIL_SERVER
Encryption=$PROTOCOL
SMTP Port=$SMTP_PORT
SMTP Username=$MAIL_USERNAME
SMTP Password=$MAIL_PASSWORD
Recipient=$RECIPIENT
Self-signed TLS/SSL certificate=$SELF_SIGNED_CERT"
# Ask if everything is okay
if ! yesno_box_yes "Does everything look correct?"
then
msg_box "OK, please start over by running this script again."
exit
fi
# Add the encryption settings to the file as well
if [ "$PROTOCOL" = "SSL" ]
then
MSMTP_ENCRYPTION1="tls on"
MSMTP_ENCRYPTION2="tls_starttls off"
elif [ "$PROTOCOL" = "STARTTLS" ]
then
MSMTP_ENCRYPTION1="tls on"
MSMTP_ENCRYPTION2="tls_starttls on"
elif [ "$PROTOCOL" = "NO-ENCRYPTION" ]
then
MSMTP_ENCRYPTION1="tls off"
MSMTP_ENCRYPTION2="tls_starttls off"
fi
# Check if auth should be set or not
if [ -z "$MAIL_USERNAME" ]
then
MAIL_USERNAME="no-reply@nextcloudvm.com"
# Without AUTH (Username and Password)
cat << MSMTP_CONF > /etc/msmtprc
# Set default values for all following accounts.
defaults
auth off
aliases /etc/aliases
$MSMTP_ENCRYPTION1
$MSMTP_ENCRYPTION2
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# logfile /var/log/msmtp
# Account to send emails
account $MAIL_USERNAME
host $MAIL_SERVER
port $SMTP_PORT
from $MAIL_USERNAME
account default : $MAIL_USERNAME
### DO NOT REMOVE THIS LINE (it's used in one of the functions in on the Nextcloud Server)
# recipient=$RECIPIENT
MSMTP_CONF
elif [ -n "$SMTP2GO" ]
then
# With AUTH (Username and Password)
cat << MSMTP_CONF > /etc/msmtprc
# Set default values for all following accounts.
defaults
auth on
aliases /etc/aliases
$MSMTP_ENCRYPTION1
$MSMTP_ENCRYPTION2
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp
# Account to send emails
account $MAIL_USERNAME
host $MAIL_SERVER
port $SMTP_PORT
from no-reply@nextcloudvm.com
user $MAIL_USERNAME
password $MAIL_PASSWORD
account default : $MAIL_USERNAME
### DO NOT REMOVE THIS LINE (it's used in one of the functions in on the Nextcloud Server)
# recipient=$RECIPIENT
MSMTP_CONF
else
# With AUTH (Username and Password)
cat << MSMTP_CONF > /etc/msmtprc
# Set default values for all following accounts.
defaults
auth on
aliases /etc/aliases
$MSMTP_ENCRYPTION1
$MSMTP_ENCRYPTION2
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp
# Account to send emails
account $MAIL_USERNAME
host $MAIL_SERVER
port $SMTP_PORT
from $MAIL_USERNAME
user $MAIL_USERNAME
password $MAIL_PASSWORD
account default : $MAIL_USERNAME
### DO NOT REMOVE THIS LINE (it's used in one of the functions in on the Nextcloud Server)
# recipient=$RECIPIENT
MSMTP_CONF
fi
# Secure the file
chmod 600 /etc/msmtprc
# Create logs
rm -f /var/log/msmtp
touch /var/log/msmtp
chmod 666 /var/log/msmtp
# Create aliases
cat << ALIASES_CONF > /etc/aliases
root: $RECIPIENT
default: $RECIPIENT
cron: $RECIPIENT
ALIASES_CONF
# Store message in a variable
TEST_MAIL="Congratulations!
Given this email reached you, it seems like everything is working properly. :)
To change the settings please check /etc/msmtprc on your server, or simply just run the setup script again.
YOUR CURRENT SETTINGS:
-------------------------------------------
$(grep -v password /etc/msmtprc)
-------------------------------------------
Best regards
The NcVM team
https://nextcloudvm.com"
# Define the mail-program
echo 'set sendmail="/usr/bin/msmtp -t"' > /etc/mail.rc
# Test mail
if ! echo -e "$TEST_MAIL" | mail -s "Test email from your NcVM" "$RECIPIENT" >> /var/log/msmtp 2>&1
then
# Set from email address
sed -i "s|from .*|from no-reply@nextcloudvm.com|g" /etc/msmtprc
MAIL_USERNAME=no-reply@nextcloudvm.com
# Second try
if ! echo -e "$TEST_MAIL" | mail -s "Test email from your NcVM" "$RECIPIENT" >> /var/log/msmtp 2>&1
then
# Test another version
echo 'set sendmail="/usr/bin/msmtp"' > /etc/mail.rc
# Third try
if ! echo -e "$TEST_MAIL" | mail -s "Test email from your NcVM" "$RECIPIENT" >> /var/log/msmtp 2>&1
then
# Fail message
msg_box "It seems like something has failed.
You can look at /var/log/msmtp for further logs.
Please run this script once more if you want to make another try or \
if you want to deinstall all newly installed packages."
exit 1
fi
fi
fi
# Success message
msg_box "Congratulations, the test email was successfully sent!
Please check the inbox for $RECIPIENT. The test email should arrive soon."
# Only offer to use the same settings in Nextcloud if a password was chosen
if [ "$MAIL_USERNAME" = "no-reply@nextcloudvm.com" ] && [ -z "$SMTP2GO" ]
then
exit
fi
# Offer to use the same settings in Nextcloud
if ! yesno_box_no "Do you want to use the same mail server settings in your Nextcloud?
If you choose 'Yes', your Nextcloud will use the same mail settings that you've entered here."
then
exit
fi
# SMTP mode
nextcloud_occ config:system:set mail_smtpmode --value="smtp"
nextcloud_occ config:system:set mail_sendmailmode --value="smtp"
# Encryption
if [ "$PROTOCOL" = "SSL" ]
then
nextcloud_occ config:system:set mail_smtpsecure --value="ssl"
elif [ "$PROTOCOL" = "STARTTLS" ]
then
nextcloud_occ config:system:set mail_smtpsecure --value="tls"
elif [ "$PROTOCOL" = "NO-ENCRYPTION" ]
then
nextcloud_occ config:system:delete mail_smtpsecure
fi
# Authentification
nextcloud_occ config:system:set mail_smtpauthtype --value="LOGIN"
nextcloud_occ config:system:set mail_smtpauth --type=integer --value=1
if [ -n "$SMTP2GO" ]
then
nextcloud_occ config:system:set mail_from_address --value="no-reply"
else
nextcloud_occ config:system:set mail_from_address --value="${MAIL_USERNAME%%@*}"
fi
if [ -n "$SMTP2GO" ]
then
nextcloud_occ config:system:set mail_domain --value="nextcloudvm.com"
else
nextcloud_occ config:system:set mail_domain --value="${MAIL_USERNAME##*@}"
fi
nextcloud_occ config:system:set mail_smtphost --value="$MAIL_SERVER"
nextcloud_occ config:system:set mail_smtpport --value="$SMTP_PORT"
nextcloud_occ config:system:set mail_smtpname --value="$MAIL_USERNAME"
nextcloud_occ config:system:set mail_smtppassword --value="$MAIL_PASSWORD"
# Show success
msg_box "The mail settings in Nextcloud were successfully set!"
# Get admin users and create menu
args=(whiptail --title "$TITLE" --menu \
"Please select the admin user that will have $RECIPIENT as mail address.
$MENU_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4)
NC_USERS_NEW=$(nextcloud_occ_no_check user:list | sed 's|^ - ||g' | sed 's|:.*||')
mapfile -t NC_USERS_NEW <<< "$NC_USERS_NEW"
for user in "${NC_USERS_NEW[@]}"
do
if nextcloud_occ_no_check user:info "$user" | cut -d "-" -f2 | grep -x -q " admin"
then
args+=("$user" "")
fi
done
choice=$("${args[@]}" 3>&1 1>&2 2>&3)
if [ -z "$choice" ]
then
msg_box "No admin user selected. Exiting."
exit 1
fi
# Set mail address for selected user
nextcloud_occ user:setting "$choice" settings email "$RECIPIENT"
# Here, it would be cool to test if sending a mail from Nextcloud works
# but this is unfortunately currently not possible via OCC, afaics
# Last message
msg_box "Congratulations, everything is now set up!"