#!/bin/bash

# get sender from site path
[ -r ${PWD%%/home/*}/site ] && domain=$(cat ${PWD%%/home/*}/site) || domain=$(echo ${PWD} | cut -d/ -f4)
sender=webmaster@${domain:-unknown.domain}

# set sendmail command
sendmail_command="/usr/bin/msmtp -i -f ${sender} -t --"

# check sender from sendmail -f option
while getopts :f: opt
do
    case "$opt" in
        f) sendmail_command="/usr/bin/formail -f -a \"From: ${OPTARG}\" -s ${sendmail_command}";;
    esac
done

# log
logger -i -p mail.info -t sendmail_wrapper uid="${UID}" pwd="${PWD}" arg="$*" cmd="${sendmail_command}"

# run sendmail command
eval ${sendmail_command}
