-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_add_email
executable file
·76 lines (67 loc) · 2.06 KB
/
postfix_add_email
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
#!/bin/sh
# Add Email to Postfix
# 08/19/17 - Created by Andrew Puckett
if [ $# -lt 2 ]; then
echo "NAME"
echo " $(basename $0) options"
echo
echo "DESCRIPTION"
echo " $(basename $0) is used to add an email address to postfix. To login with gmail use mail.domain, full email address and the password generated by this script. Use TLS on port 587."
echo
echo "OPTIONS"
echo " -d"
echo " Domain. Required."
echo " -e"
echo " Email address (e.g. firstname). This will be forwarded to admin unless -f is used. Required."
echo " -i"
echo " Don't setup sasl. Can be used if importing the user from the sasldb2 file."
echo " -f"
echo " Forward the extra email address (e.g. example@gmail.com)."
echo " -p"
echo " Create a new SMTP password for extra email address."
echo
echo "AUTHOR"
echo " Andrew Puckett"
echo
exit 1
fi
[ $(id -u) != "0" ] && echo "ERROR: You must be the superuser to run this script" >&2 && exit 1
DOMAIN=
EMAIL=
FORWARD=
CREATE_PASS=0
SASL=1
while getopts d:e:f:ip opt
do
case "$opt" in
d) DOMAIN=$OPTARG;;
e) EMAIL=$OPTARG;;
f) FORWARD=$OPTARG;;
i) SASL=0;;
p) CREATE_PASS=1;;
esac
done
[ -n "$FORWARD" ] || FORWARD=admin@$DOMAIN
[ -f /etc/postfix/virtual ] && cat /etc/postfix/virtual > /etc/postfix/virtual_new
echo $EMAIL@$DOMAIN $FORWARD >> /etc/postfix/virtual_new
mv --backup=numbered /etc/postfix/virtual_new /etc/postfix/virtual
postmap /etc/postfix/virtual
[ $SASL -eq 0 ] && exit
if [ $CREATE_PASS -eq 1 ]; then
PASS=$(openssl rand -base64 16 | tr / +)
echo "####################################################################################################"
echo $EMAIL@$DOMAIN password: $PASS
echo "####################################################################################################"
else
st=$(stty -g)
#trap "stty $st; exit" HUP INT QUIT TERM
stty -echo
read -p "$EMAIL password: " PASS
stty $st
echo
fi
echo $PASS | saslpasswd2 -c -u $DOMAIN $EMAIL -p
cp /etc/sasldb2 /var/spool/postfix/etc/
chown postfix:postfix /var/spool/postfix/etc/sasldb2
service postfix restart
systemctl daemon-reload