Created
January 8, 2026 08:02
-
-
Save sharmashivanand/2eb35a0f9171dbbf385be690eab840bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # run like: bash setup-postfix-outbound.sh someone@somewhere.com | |
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| TO="${1:-}" | |
| if [[ -z "${TO}" ]]; then | |
| echo "Usage: $0 recipient@example.com" | |
| exit 1 | |
| fi | |
| # Re-run as root if needed | |
| if [[ "${EUID}" -ne 0 ]]; then | |
| exec sudo -E bash "$0" "$TO" | |
| fi | |
| export DEBIAN_FRONTEND=noninteractive | |
| apt-get update -y | |
| apt-get install -y postfix bsd-mailx ca-certificates | |
| # Ensure a main config exists (some broken images/templates may be missing it) | |
| if [[ ! -f /etc/postfix/main.cf ]]; then | |
| if [[ -f /etc/postfix/main.cf.proto ]]; then | |
| cp /etc/postfix/main.cf.proto /etc/postfix/main.cf | |
| else | |
| # last resort: create a minimal config file | |
| postconf -n > /etc/postfix/main.cf | |
| fi | |
| fi | |
| # Remove known "blank value" keys that can crash postfix if they exist as "key =" | |
| for k in debugger_command html_directory manpage_directory sample_directory readme_directory; do | |
| if grep -Eq "^${k}[[:space:]]*=[[:space:]]*$" /etc/postfix/main.cf; then | |
| postconf -X "$k" || true | |
| fi | |
| done | |
| # Required paths/groups (avoids template blanks like setgid_group =) | |
| postconf -e "setgid_group=postdrop" | |
| postconf -e "sendmail_path=/usr/sbin/sendmail" | |
| postconf -e "newaliases_path=/usr/bin/newaliases" | |
| postconf -e "mailq_path=/usr/bin/mailq" | |
| HN="$(hostname -f 2>/dev/null || hostname)" | |
| DM="$(hostname -d 2>/dev/null || true)" | |
| postconf -e "myhostname=${HN}" | |
| if [[ -n "${DM}" ]]; then | |
| postconf -e "mydomain=${DM}" | |
| fi | |
| # Outbound-only (no listening on public interfaces) | |
| postconf -e "inet_interfaces=loopback-only" | |
| postconf -e "inet_protocols=ipv4" | |
| # Keep local delivery sane (so DSNs don’t try weird remote routes) | |
| postconf -e 'mydestination=$myhostname, localhost.$mydomain, localhost' | |
| systemctl enable --now postfix | |
| postfix check | |
| SUBJ="postfix outbound test $(date -Is)" | |
| BODY="Test email via Postfix from ${HN} at $(date -Is)." | |
| printf "Subject: %s\nTo: %s\nFrom: root@%s\n\n%s\n" \ | |
| "${SUBJ}" "${TO}" "${HN}" "${BODY}" | sendmail -t -v | |
| echo "Sent. If it doesn't arrive, check:" | |
| echo " journalctl -u postfix -n 80 --no-pager" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment