-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-client.sh
executable file
·60 lines (51 loc) · 1.73 KB
/
smtp-client.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
#!/usr/bin/env bash
### SMTP client for command line ### @RCaldas84
### Get current script dir
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
### Load settings from .env file or uncomment next:
source "$DIR/.env"
# SMTP_SERVER='server.domain'
# SMTP_PORT=587
# SMTP_USER='user@mail.com'
# SMTP_PWD='userpassword'
# DOMAIN='domain.com'
# ADMIN_MAIL='your@mail.com'
### Set /etc/mailname
echo "$DOMAIN" | sudo tee /etc/mailname >/dev/null
### Check for packages
for p in 'postfix' 'mailutils'; do
if ! dpkg -s "$p" &> /dev/null; then
sudo apt update
sudo apt install -y "$p"
fi
done
### Create root alias
grep -q '^root:' /etc/aliases && \
sudo sed -i "/^root:/c\root: $ADMIN_MAIL" /etc/aliases || \
echo "root: $ADMIN_MAIL" | sudo tee -a /etc/aliases >/dev/null
sudo newaliases
### Set main.cf file
[ ! -e /etc/postfix/main.cf.bkp ] && \
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bkp
cat <<EOF | sudo tee /etc/postfix/main.cf >/dev/null
myhostname = $DOMAIN
inet_interfaces = loopback-only
relayhost = [$SMTP_SERVER]:$SMTP_PORT
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
mynetworks_style = host
EOF
### Set sasl_passwd file
echo "[$SMTP_SERVER]:$SMTP_PORT $SMTP_USER:$SMTP_PWD" | \
sudo tee /etc/postfix/sasl_passwd >/dev/null
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
### Set MAILTO in crontab, replace if exist or add at top:
grep -q 'MAILTO=' /etc/crontab && \
sudo sed -i "/MAILTO=/c\MAILTO=$ADMIN_MAIL" /etc/crontab || \
sudo sed -i "1iMAILTO=$ADMIN_MAIL" /etc/crontab
### Restart postfix
sudo systemctl restart postfix