# ====================
# Forward E-Mails to 'do-not-reply@..' address to (file) /dev/null
# ====================
# ---
# Example for this readme:
#
# Create a forwarding to /dev/null for the address no-reply@cloud-01.oopen.de
# ---
DOMAIN=""cloud-01.oopen.de
E_MAIL="no-reply@cloud-01.oopen.de"
# see also:
# https://www.postfix.org/VIRTUAL_README.html
#
# https://think.unblog.ch/weiterleiten-von-postfix-alias-an-dev-null/
# https://think.unblog.ch/en/forward-postfix-alias-to-dev-null/
#
# https://www.serverwatch.com/guides/forwarding-a-postfix-virtual-alias-to-dev-null/
# Notice:
# the usual solution is to forward to (file) /dev/null. In a local-only setup you could do
# that in /etc/aliases.
#
# However, if your're using Postfix virtual domains, it gets a little more complicated. With
# virtual domains/users, you can't forward mail to a file (like /dev/null).
# ---
# 1. Set up an alias user 'do-not-reply'that forwards incoming e-mails to /dev/null.
# ---
# Add to file '/etc/aliases' a line like:
# do-not-reply: /dev/null
if ! $(grep -q -E "^\s*do-not-reply:" /etc/aliases 2>/devnull) ; then
cat <<EOF >> /etc/aliases
do-not-reply: /dev/null
EOF
fi
# Renew alias database:
#
newaliases
# ---
# 2. Set up the domain (domain part of not-reply address)
# ---
# Create file '/etc/postfix/virtual_alias_domains' and add your domain '@cloud-01.oopen.de'
#
# ${DOMAIN} OK
#
if [[ ! -f "/etc/postfix/virtual_alias_domains" ]] ; then
cat <<EOF >> /etc/postfix/virtual_alias_domains
# - File: /etc/postfix/virtual_alias_domains
# -
# - Note:
# - a mapping file always has two columns. In such a 'one-dimensional' mapping (list of domains)
# - Postfix does not care about your second column. It does not even have to be 'OK', it can be
# - anything.
# -
# - Example:
# - <domain> OK
${DOMAIN} OK
EOF
else
if ! $(grep -q -E "^\s*${DOMAIN}" /etc/postfix/virtual_alias_domains 2> /dev/null) ; then
cat <<EOF >> /etc/postfix/virtual_alias_domains
${DOMAIN} OK
EOF
fi
fi
# Create Postfix lookup table (database file) from '/etc/postfix/virtual_alias_domains'
#
postmap btree:/etc/postfix/virtual_alias_domains
# Add virtual_alias_domains to Postfix main Configuration
#
# ...
# virtual_alias_domains =
# btree:/etc/postfix/virtual_alias_domains
# ...
#
if ! $(grep -q -E "^\s*virtual_alias_domains\s*=\s*btree:/etc/postfix/virtual_alias_domains" /etc/postfix/main.cf) \
&& ! $(grep -q -E "^\s*btree:/etc/postfix/virtual_alias_domains" /etc/postfix/main.cf); then
perl -i -n -p -e "s#^(\s*virtual_alias_domains\s*=.*)#\1\n btree:/etc/postfix/virtual_alias_domains#" /etc/postfix/main.cf
fi
# ---
# 3. Set up adress mapping
# ---
# - Create file '/etc/postfix/virtual_alias_maps' and add your address mapping
# -
# - ${E_MAIL} do-not-reply
# -
if [[ ! -f "/etc/postfix/virtual_alias_maps" ]] ; then
cat <<EOF >> /etc/postfix/virtual_alias_maps
# - File: /etc/postfix/virtual_alias_maps
# -
# - Redirect mail for one address to one or more addresses.
# -
# - Example:
# - # incomming address 'no-reply@cloud-01.oopen.de' to local 'do-not-reply' address
# - no-reply@cloud-01.oopen.de do-not-reply
# -
${E_MAIL} do-not-reply
EOF
else
if ! $(grep -q -E "^\s*${E_MAIL}" /etc/postfix/virtual_alias_maps 2> /dev/null) ; then
cat <<EOF >> /etc/postfix/virtual_alias_maps
${E_MAIL} do-not-reply
EOF
fi
fi
# Create Postfix lookup table (database file) from '/etc/postfix/virtual_alias_maps'
#
postmap btree:/etc/postfix/virtual_alias_maps
# Add virtual_alias_maps to Postfix main Configuration
#
# ...
# virtual_alias_maps =
# btree:/etc/postfix/virtual_alias_maps
# ...
#
if ! $(grep -q -E "^\s*virtual_alias_maps\s*=\s*btree:/etc/postfix/virtual_alias_maps" /etc/postfix/main.cf) \
&& ! $(grep -q -E "^\s*btree:/etc/postfix/virtual_alias_maps" /etc/postfix/main.cf); then
perl -i -n -p -e "s#^(\s*virtual_alias_maps\s*=.*)#\1\n btree:/etc/postfix/virtual_alias_maps#" /etc/postfix/main.cf
fi
# ---
# 4. Reload Postfix
# ---
systemctl reload postfix