#!/bin/bash

source /opt/scripts_local/shared_functions.sh

#from address smtp@inxs.be
export DEST_EMAIL="tgram-sysadmin@inxs.me"
export SRC_EMAIL="sysadmin@inxs.me"
export RELAY_SRV="[mailrise.inxs.me]:2525"
HOSTNAME=$(hostname -f)
DOMAINNAME=inxs.me

sudo apt install -y postfix libsasl2-modules bsd-mailx

bck_file () { sudo mv "$1"{,.backup_"$(date +%Y%m%d_%H%M)"}; }
bck_file /etc/aliases  >/dev/nul 2>&1
bck_file /etc/postfix/sasl_passwd >/dev/nul 2>&1
bck_file /etc/postfix/main.cf >/dev/nul 2>&1


sudo bash -c 'cat > /etc/postfix/sasl_passwd' <<EOF
$RELAY_SRV
EOF

sudo bash -c 'cat > /etc/postfix/main.cf' <<EOF
smtpd_banner = $HOSTNAME ESMTP $DOMAINNAME (Debian/GNU)
biff = no
append_dot_mydomain = no
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $HOSTNAME, localhost.$DOMAINNAME, localhost, $DOMAINNAME
mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only
recipient_delimiter = +
compatibility_level = 2
relayhost = $RELAY_SRV
smtp_tls_security_level = may
header_size_limit = 4096000
smtp_use_tls = no
smtp_sasl_auth_enable = no
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtpd_tls_loglevel = 1
EOF

sudo bash -c 'cat > /etc/aliases' <<EOF
abuse: root
ftp: root
hostmaster: root
mailer-daemon: root
netdata: root
news: root
nobody: root
noc: root
postmaster: root
security: root
usenet: root
webmaster: root
www: root
root: $DEST_EMAIL
EOF

sudo newaliases
# postmap /etc/postfix/sasl_passwd 
sudo systemctl restart postfix 

sudo chmod 0600  /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db >/dev/null 2>&1
sudo chmod 0755  /etc/postfix/
chmod o+r /etc/postfix/main.cf

echo "${HOSTNAME} this is a test email." | mailx -r $SRC_EMAIL -s "This is a test message from ${HOSTNAME}" "$DEST_EMAIL"









