Files
docker-skeleton/.utils/mail/cronmail
T
2026-05-02 23:46:29 +02:00

53 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
#
# This is a sendmail to mailx format converter script.
# It receives a well-formed email text from standard input, parses it,
# and invokes a POSIX mailx compliant mail user agent with the result.
#
# It is primarily designed to use with crond. To activate
# use CRONDARGS="-m pathname_to_cronmail" in/etc/default/cron or
# /etc/sysconfig/crond configuration.
#
# Author: Kovács Zoltán <kovacsz@marcusconsulting.hu>
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
# 2026-05-02 v0.1 Initila release.
# Initialisations.
#
SUBJECT=""
RECIPIENT="$USER" # Default recipient.
MESSAGE=""
MUA="/usr/local/bin/mail" # Custom mail user agent script.
# Parsing the stdin line by line.
#
while IFS= read -r line
do
# Parses the recipient.
if [[ "$line" =~ ^(T|t)o:\ .*$ ]]; then
RECIPIENT="${line#*: }"
# Parses the subject line.
elif [[ "$line" =~ ^(S|s)ubject:\ .*$ ]]; then
SUBJECT="${line#*: }"
# Message body starts with an empty line.
elif [[ "$line" =~ ^$ ]]; then
MESSAGE+="\n"
# Collects the message body lines.
# The first empty line will be stripped.
elif [ -n "$MESSAGE" ]; then
[[ "$MESSAGE" = "\n" ]] \
&& MESSAGE="$line\n" \
|| MESSAGE+="$line\n"
fi
done
# Calls the mail user agent.
#
if [ -x "$MUA" ]; then
if [ -n "$SUBJECT" -o -n "$MESSAGE" ]; then
echo -e "$MESSAGE" | "$MUA" -s "$SUBJECT" "$RECIPIENT"
fi
fi
# That's all, Folks! :)