postfix/run.sh
turboaaa e7eb0a9d8e
Add header tag creation.
This will create a tag in email headers that can be used for filtering by receiving servers. I use this image for relaying through exchange online, and the nature of how I run my containers prevents me from setting static IPs as a filter. With a tag for exchange to look at, I can make sure emails from the relay are not hitting the junk folder.

Without setting the HEADER_TAG variable, the script will create a randomly generated tag and move on. It is not a requirement to make use of this feature.
2019-01-16 12:34:48 -05:00

71 lines
2.5 KiB
Bash

#!/bin/bash
[ "${DEBUG}" == "yes" ] && set -x
function add_config_value() {
local key=${1}
local value=${2}
local config_file=${3:-/etc/postfix/main.cf}
[ "${key}" == "" ] && echo "ERROR: No key set !!" && exit 1
[ "${value}" == "" ] && echo "ERROR: No value set !!" && exit 1
echo "Setting configuration option ${key} with value: ${value}"
sed -i -e "/^#\?\(\s*${key}\s*=\s*\).*/{s//\1${value}/;:a;n;:ba;q}" \
-e "\$a${key}=${value}" \
${config_file}
}
[ -z "${SMTP_SERVER}" ] && echo "SMTP_SERVER is not set" && exit 1
[ -z "${SMTP_USERNAME}" ] && echo "SMTP_USERNAME is not set" && exit 1
[ -z "${SMTP_PASSWORD}" ] && echo "SMTP_PASSWORD is not set" && exit 1
[ -z "${SERVER_HOSTNAME}" ] && echo "SERVER_HOSTNAME is not set" && exit 1
SMTP_PORT="${SMTP_PORT-587}"
#Get the domain from the server host name
DOMAIN=`echo ${SERVER_HOSTNAME} |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//'`
# Set needed config options
add_config_value "myhostname" ${SERVER_HOSTNAME}
add_config_value "mydomain" ${DOMAIN}
add_config_value "mydestination" '$myhostname'
add_config_value "myorigin" '$mydomain'
add_config_value "relayhost" "[${SMTP_SERVER}]:${SMTP_PORT}"
add_config_value "smtp_use_tls" "yes"
add_config_value "smtp_sasl_auth_enable" "yes"
add_config_value "smtp_sasl_password_maps" "hash:\/etc\/postfix\/sasl_passwd"
add_config_value "smtp_sasl_security_options" "noanonymous"
# Create sasl_passwd file with auth credentials
if [ ! -f /etc/postfix/sasl_passwd ]; then
grep -q "${SMTP_SERVER}" /etc/postfix/sasl_passwd > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "Adding SASL authentication configuration"
echo "[${SMTP_SERVER}]:${SMTP_PORT} ${SMTP_USERNAME}:${SMTP_PASSWORD}" >> /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
fi
fi
#Set header tag
postconf -e "header_checks = regexp:/etc/postfix/header_tag"
if [ -z "${HEADER_TAG}" ]; then
TAG="$RANDOM"
else
TAG="${HEADER_TAG}"
fi
echo -e "/^MIME-Version:/i PREPEND RelayTag: $TAG\n/^Content-Transfer-Encoding:/i PREPEND RelayTag: $TAG" > /etc/postfix/header_tag
echo "******** Header tag is $TAG *********"
# Create sasl_passwd file with auth credentials
if [ ! -f /etc/postfix/sasl_passwd ]; then
grep -q "${SMTP_SERVER}" /etc/postfix/sasl_passwd > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "Adding SASL authentication configuration"
echo "[${SMTP_SERVER}]:${SMTP_PORT} ${SMTP_USERNAME}:${SMTP_PASSWORD}" >> /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
fi
fi
#Start services
supervisord