Forums updated to SMF version 2.1.1

sending email as html messes up formatting

Started by aristosv, March 13, 2022, 04:00:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


This script goes through a google calendar, picks up any events that contain phone numbers starting with 95, 96, 97, 99 and emails a report to $emailaddress.

# emails $emailaddress a list of client appointments for the $currentdate
# sources $emailaddress and $currentdate from operations/variables
# sources $clientname from $clientdir/config

parentdir=$(dirname "$(dirname "$(readlink -f -- "$0")")")
source "$parentdir/operations/variables"

get_events () {
echo "Reminders Daily Report for $currentdate"
for clientdir in $(ls $parentdir/clients -F | grep \/$ | sed 's#/$##') ; do
source "$parentdir/clients/$clientdir/config"
echo "$clientname Reminders $currentdate"
gcalcli --config-folder $parentdir/clients/$clientdir/auth agenda --tsv \
"$(date -d 'now')" "$(date -d 'now + 24 hours')" \
| grep -e 95 -e 96 -e 97 -e 99 \
| awk '{$1=$3=$4=""; print $0}'

send_mail () {
mail -a 'Content-Type: text/html' -s "Reminders Daily Report" $emailaddress

"get_events" | send_mail

My problem is that the report arrives in the email, in a single line, like this:

Reminders Daily Report for 13/03/2022 Client1 Reminders 13/03/2022 21:00 991234567 21:30 991234567 Client2 Reminders 13/03/2022 21:00 991234567 21:30 991234567

and I want it to arrive in the email, like this:

Reminders Daily Report for 13/03/2022

Client1 Reminders 13/03/2022
21:00   991234567
21:30   991234567

Client2 Reminders 13/03/2022
21:00   991234567
21:30   991234567

The problem is resolved when I change "Content-Type: text/html" to "Content-Type: text/plain", but I have to send this as html because I'm planning on adding some html code in the script.

How can I fix this?

Mad Penguin

In text mode the receiver will interpret textual line endings, so if you have a "\n"  (ASCII 10) in your text, it will show a line break. In html mode however, whitespace (i.e. spaces, newlines, tabs) tends to be mangled according to "html"'s idea of formatting and can't be relied upon. To guarantee a line break you will need to insert HTML into your output. There are a number of ways to get a line break, in your case the easiest is probably to insert a "< br / >" into your output instead of a "\n". (that's a br/ enclosed within chevrons, no spaces)