Sending FreeBSD script output by email loses format (left aligned and right aligned in one line)
every day I am running a script on my router to check the traffic usage of a specific server. The script is working fine and with this output part of the script, I get following result:
#Give output
printf "Traffic Report for all Servers"; printf "%10s\n"
echo -e ""
printf "## Traffic Server 1 ##"; printf "%10s\n"
echo "--------------------------------------"
printf "Traffic User1 :"; printf "%15s\n" "1200.00 GB"
printf "Traffic Client123:"; printf "%12s\n" "385.00 GB"
printf "Traffic Max:"; printf "%18s\n" "0 GB"
printf "Traffic Smith:"; printf "%16s\n" "987.00 GB"
printf "Traffic Johanna:"; printf "%14s\n" "30.00 GB"
printf "Traffic Other:"; printf "%16s\n" "8.97 GB"
echo "--------------------------------------"
printf "Traffic Gesamt DE:"; printf "%12s\n" "1524.00 GB"
printf "Traffic Quota DE:"; printf "%13s\n" "2000.00 GB"
printf "Traffic Rest DE:"; printf "%14s\n" "500.00 GB"
Traffic Report for all Servers
## Traffic Server 1 ##
--------------------------------------
Traffic User1 : 1200.00 GB
Traffic Client123: 385.00 GB
Traffic Max: 0 GB
Traffic Smith: 987.00 GB
Traffic Johanna: 30.00 GB
Traffic Other: 8.97 GB
--------------------------------------
Traffic Gesamt DE: 1524.00 GB
Traffic Quota DE: 2000.00 GB
Traffic Rest DE: 500.00 GB
So far everything is correct, I want exactly to see this output. But if I use the script to send this output via email with ssmtp
#!/bin/sh
#set -x
#Give output
{
printf "Traffic Report for all Servers"; printf "%10s\n"
echo -e ""
printf "## Traffic Server 1 ##"; printf "%10s\n"
echo "--------------------------------------"
printf "Traffic User1 :"; printf "%15s\n" "1200.00 GB"
printf "Traffic Client123:"; printf "%12s\n" "385.00 GB"
printf "Traffic Max:"; printf "%18s\n" "0 GB"
printf "Traffic Smith:"; printf "%16s\n" "987.00 GB"
printf "Traffic Johanna:"; printf "%14s\n" "30.00 GB"
printf "Traffic Other:"; printf "%16s\n" "8.97 GB"
echo "--------------------------------------"
printf "Traffic Gesamt DE:"; printf "%12s\n" "1524.00 GB"
printf "Traffic Quota DE:"; printf "%13s\n" "2000.00 GB"
printf "Traffic Rest DE:"; printf "%14s\n" "500.00 GB"
} | ssmtp -F Sender [email protected]
#End
the result looks totally different, as the output in the terminal.
Traffic Report for all Servers
## Traffic Server 1 ##
--------------------------------------
Traffic User1 : 1200.00 GB
Traffic Client123: 385.00 GB
Traffic Max: 0 GB
Traffic Smith: 987.00 GB
Traffic Johanna: 30.00 GB
Traffic Other: 8.97 GB
--------------------------------------
Traffic Gesamt DE: 1524.00 GB
Traffic Quota DE: 2000.00 GB
Traffic Rest DE: 500.00 GB
It's strange, I just found out that if I copy the text from the email to this text box here, the text is correctly aligned, but I just see it in the email like above.
Does anyone have an idea how to get the output same style as in the email? Thank you
Solution 1:
It's strange, I just found out that if I copy the text from the email to this text box here, the text is correctly aligned, but I just see it in the email like above.
The text is plain text where "formatting" is done by consecutive spaces. There is no information that some part should align to the right. There are only spaces that precede the right part. The spaces make it align to the right nicely if the font is a fixed-width font.
Terminals and terminal emulators commonly use fixed-width fonts. This way not only a text like yours can be displayed correctly; simple boxes can be drawn, more complex layouts (like the interface of mc
) can be drawn. If a character of a different width is rendered, it may break the layout (example).
GUI programs that work with code use fixed-width fonts (because indentation, strings and code in general look better this way, we know this by experience), but general-purpose GUI programs usually use variable-width fonts (because literature looks better this way). Apparently your e-mail client uses a variable-width font to show plain text to you.
In the e-mail in question all the spaces are there; if you paste the text to a terminal then it will align correctly; the problem is spaces are not as wide as letters and digits (and some letters are not as wide as others) when you view the e-mail.
We can see the difference even here1. This site uses a fixed-width font for codeblocks:
Example 1
Traffic Client123: 385.00 GB
Traffic Max: 0 GB
It uses a variable-width font for text:
Example 2
Traffic Client123: 385.00 GB
Traffic Max: 0 GB
It can be even worse. In the example 2 I replaced regular ASCII spaces with non-breaking spaces because the site treats consecutive regular spaces as one and if I didn't counteract then it would look like this:
Example 3
Traffic Client123: 385.00 GB
Traffic Max: 0 GB
Your e-mail client probably does not compact consecutive spaces, it renders text like in the example 2. Still the alignment is not like in the example 1.
Configure your e-mail client to use a fixed-width font for plain text. If you cannot or don't want to, you will need to paste misaligned text from e-mails to a text editor to enjoy the alignment. It may be a GUI editor, if configured to use a fixed-width font; or basically any text editor in a terminal emulator (unless your terminal emulator itself is configured to use a variable-width font, this is unlikely though).
Alternatively, if your e-mail client can render e-mails formatted as HTML, modify your script to send HTML instead of plain text. I believe you can specify font in HTML (obviously in this case you want a fixed-width font); or you can align using directives, not spaces. I don't really know HTML but I have found this: How may I align text to the left and text to the right in the same line? Note your options are limited by what your e-mail client can interpret.
1 Different browsers in different systems may use different fonts; additionally a browser may be configured to use a font different than the site requested. Therefore you may or may not see my examples in the way I intended. Just in case, the below screenshot shows the examples as they are intended to look (click to see the full size):