embed the template in a function

sed is not very good with multi-line search/replace, so we've decided to 
use a shell function to format the mail.
This commit is contained in:
Jérémy Lecour 2019-03-18 14:15:09 +01:00 committed by Jérémy Lecour
parent b8b94a66ce
commit bea327a254
1 changed files with 47 additions and 15 deletions

View File

@ -204,22 +204,54 @@ hook_db() {
fi
}
format_mail() {
cat <<EOTEMPLATE
From: ${FULLFROM}
Content-Type: text/plain; charset=UTF-8
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
To: ${EVOMAINTMAIL}
Subject: [evomaintenance] Intervention sur ${HOSTNAME_TEXT} (${USER})
Bonjour,
Une intervention vient de se terminer sur votre serveur.
Nom du serveur : ${HOSTNAME_TEXT}
Personne ayant réalisée l'intervention : ${USER}
Intervention réalisée depuis : ${IP}
Début de l'intervention : ${BEGIN_DATE}
Fin de l'intervention : ${END_DATE}
### Renseignements sur l'intervention
${MESSAGE}
###
EOTEMPLATE
if [ -n "${GIT_COMMITS}" ]; then
cat << EOTEMPLATE
### Commits
${GIT_COMMITS}
###
EOTEMPLATE
fi
cat <<EOTEMPLATE
Pour réagir à cette intervention, vous pouvez répondre à ce message
(sur l'adresse mail ${FROM}). En cas d'urgence, utilisez
l'adresse ${URGENCYFROM} ou notre téléphone portable d'astreinte
(${URGENCYTEL})
Cordialement,
--
${FULLFROM}
EOTEMPLATE
}
hook_mail() {
MAIL_TEXTE=$(echo "${MESSAGE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
MAIL_CONTENT=$(sed -e "s/__TO__/${EVOMAINTMAIL}/" \
-e "s/__HOSTNAME__/${HOSTNAME_TEXT}/" \
-e "s/__USER__/${USER}/" \
-e "s/__BEGIN_DATE__/${BEGIN_DATE}/" \
-e "s/__END_DATE__/${END_DATE}/" \
-e "s/__GIT_COMMITS__/${MAIL_GIT_COMMITS}/" \
-e "s/__TEXTE__/${MAIL_TEXTE}/" \
-e "s/__IP__/${IP}/" \
-e "s/__FULLFROM__/${FULLFROM}/" \
-e "s/__FROM__/${FROM}/" \
-e "s/__URGENCYFROM__/${URGENCYFROM}/" \
-e "s/__URGENCYTEL__/${URGENCYTEL}/" \
/usr/share/scripts/evomaintenance.tpl)
MAIL_CONTENT=$(format_mail)
if [ "${VERBOSE}" = "1" ]; then
printf "\n********** Mail *******************\n%s\n***********************************\n" "${MAIL_CONTENT}"