forked from loganmarchione/docker-postfixrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
100 lines (86 loc) · 3.47 KB
/
entrypoint.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
#!/bin/sh -e
printf "#####\n"
printf "# Container starting up!\n"
printf "#####\n"
# Test variables for timezone
if [ -z "$TZ" ]; then
printf "# ERROR: TZ is undefined, exiting!\n"
else
printf "# STATE: Setting container timezone to $TZ\n"
ln -sf /usr/share/zoneinfo/"$TZ" /etc/localtime
echo "$TZ" > /etc/timezone
fi
# Test variables for relay
if [[ -z "$RELAY_HOST" || -z "$RELAY_PORT" ]]; then
printf "# ERROR: Either RELAY_HOST or RELAY_PORT are undefined, exiting!\n"
exit 1
fi
# Create directories
printf "# STATE: Changing permissions\n"
postfix set-permissions
# Set logging
printf "# STATE: Setting Postfix logging to stdout\n"
postconf -e "maillog_file = /dev/stdout"
# Configure Postfix
printf "# STATE: Configuring Postfix\n"
postconf -e "inet_interfaces = all"
postconf -e "mydestination ="
postconf -e "mynetworks = ${MYNETWORKS:=0.0.0.0/0}"
postconf -e "relayhost = [$RELAY_HOST]:$RELAY_PORT"
# Set the "from" domain, needed for things like AWS SES
if [[ -z "$MYORIGIN" ]]; then
printf "# ERROR: MYORIGIN is undefined, continuing\n"
else
printf "# STATE: MYORIGIN is defined as $MYORIGIN\n"
postconf -e "myorigin = $MYORIGIN"
fi
# Set the "from" address, needed for some SMTP providers
# https://serverfault.com/questions/147921/forcing-the-from-address-when-postfix-relays-over-smtp
if [[ -z "$FROMADDRESS" ]]; then
printf "# ERROR: FROMADDRESS is undefined, continuing\n"
else
printf "# STATE: FROMADDRESS is defined as $FROMADDRESS\n"
postconf -e "smtp_header_checks = regexp:/etc/postfix/header_checks"
echo "/^From:.*/ REPLACE From: $FROMADDRESS" | tee /etc/postfix/header_checks > /dev/null
postconf -e "sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps"
echo "/.+/ $FROMADDRESS" | tee /etc/postfix/sender_canonical_maps > /dev/null
fi
# Set the message_size_limit
if [[ -z "$MSG_SIZE" ]]; then
printf "# ERROR: MSG_SIZE is undefined, continuing\n"
else
printf "# STATE: MSG_SIZE is defined as $MSG_SIZE\n"
postconf -e "message_size_limit = $MSG_SIZE"
fi
# Client settings (for sending to the relay)
postconf -e "smtp_tls_security_level = encrypt"
postconf -e "smtp_tls_loglevel = 1"
postconf -e "smtp_tls_note_starttls_offer = yes"
postconf -e "smtp_sasl_auth_enable = yes"
postconf -e "smtp_sasl_security_options = noanonymous"
postconf -e "smtp_sasl_password_maps = lmdb:/etc/postfix/sasl_passwd"
postconf -e "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt"
# Create password file
# Alpine 3.13 dropped support for Berkeley DB, so using lmdb instead
# https://wiki.alpinelinux.org/wiki/Release_Notes_for_Alpine_3.13.0#Deprecation_of_Berkeley_DB_.28BDB.29
echo "[$RELAY_HOST]:$RELAY_PORT $RELAY_USER:$RELAY_PASS" > /etc/postfix/sasl_passwd
chown root:root /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd
postmap lmdb:/etc/postfix/sasl_passwd
rm -f /etc/postfix/sasl_passwd
chown root:root /etc/postfix/sasl_passwd.lmdb
chmod 600 /etc/postfix/sasl_passwd.lmdb
# Rebuild the database for the mail aliases file
newaliases
# Send test email
# Test for variable and queue the message now, it will send when Postfix starts
if [[ -z "$TEST_EMAIL" ]]; then
printf "# ERROR: TEST_EMAIL is undefined, continuing without a test email\n"
else
printf "# STATE: Sending test email\n"
echo -e "Subject: Postfix relay test \r\nTest of Postfix relay from Docker container startup\nSent on $(date)\n" | sendmail -F "[Alert from Postfix]" "$TEST_EMAIL"
fi
# Start Postfix
# Nothing else can log after this
printf "# STATE: Starting Postfix\n"
postfix start-fg