#!/bin/sh set -e # Generate main.cf from template with environment variables envsubst < /etc/postfix/main.cf.template > /etc/postfix/main.cf # Generate MySQL virtual alias config from template envsubst < /etc/postfix/mysql_virtual_alias_maps.cf.template > /etc/postfix/mysql_virtual_alias_maps.cf # Generate SASL password file from environment variables envsubst < /etc/postfix/sasl_passwd.template > /etc/postfix/sasl_passwd # Wait for MySQL to be ready echo "Waiting for MySQL to be ready..." for i in $(seq 1 30); do if nc -z ${MYSQL_HOST} ${MYSQL_PORT} 2>/dev/null; then echo "MySQL is ready!" break fi echo "Waiting for MySQL... ($i/30)" sleep 2 done # Generate Postfix hash databases postmap /etc/postfix/sasl_passwd chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db # Regenerate sender_access database postmap /etc/postfix/sender_access chmod 644 /etc/postfix/sender_access /etc/postfix/sender_access.db # Set permissions on MySQL config chmod 644 /etc/postfix/mysql_virtual_alias_maps.cf # Start Postfix in foreground exec postfix start-fg