Descriptive Answer
Includes changes per @StéphaneChazelas comments as well as Answer
The general problem is that most modern Mail User Agent (MUA) clients do not display plain text in Monospace formatting. Stand alone clients, such as Gnome's Evolution, Microsoft Outlook, and Mozilla's Thunderbird utilize various fonts. While most webmail clients, such as Gmail, expect and display HTML. Fortunately, there is an HTML tag for partitioning out a block of pre-formatted text. This tag set is, conveniently, <pre></pre>
. Unfortunately, Unix mail
sends plain text email by default. So, there are two problems to solve:
- Modify the sent file to include the
<pre></pre>
HTML tags.
- Change the
content-type
of the sent email to text/html
.
Adding the HTML tags is a simple process of adding the <pre>
tag before the first line and the </pre>
tag after the last line of the file to be sent. I've borrowed the method of sending HTML email from this anwser.
Functional Script
Please note this is a very basic non-production ready script with no error checking:
#!/bin/bash
# Requires GNU recode
# Usage:
# ./email_log.sh file_to_send.txt subject recipient
# Set paths and filenames
_dir="."
_infile=$1
_subject=$2
_user=$3
_sendfile="$_dir/send.txt"
# Prepend and append <pre></pre> HTML tags
cat $_infile |recode ..html |sed "1s;^;To: $_user\nSubject: $_subject\nContent-Type: text/html\n<html><body><pre>\n;" > $_sendfile
echo "</pre></body></html>" >> $_sendfile
# Sending html email
cat $_sendfile | /usr/sbin/sendmail -t -oi
# Cleanup
# rm $_sendfile
Execute as follows:
./email_log.sh test_lines.txt "This is a test of sending a text file using <pre> html tags" user1
Complete email with headers showing the content-type is text/html
and the <pre>
tags have been added:
Return-path: <user1@host>
Envelope-to: user1@host
Delivery-date: Fri, 05 Apr 2019 09:39:03 -0400
Received: from user1 by host with local (Exim 4.92)
(envelope-from <user1@host>)
id 1hCP3f-0000Ie-T6
for user1@host; Fri, 05 Apr 2019 09:39:03 -0400
Subject: This is a test of sending a text file using <pre> html tags
Content-Type: text/html
To: <user1@host>
X-Mailer: mail (GNU Mailutils 3.5)
Message-Id: <E1hCP3f-0000Ie-T6@pots>
From: user1 <user1@host>
Date: Fri, 05 Apr 2019 09:39:03 -0400
X-Evolution-Source: 2e20a156d92decafcdd72e4d7b87e28dd95ed39a
MIME-Version: 1.0
<pre>
1234567890 1234567890
1234
123456
Do not wrap around this line please. Do not wrap around this line please. Do not wrap around this line please. Do not wrap around this line please.
1234567890
_________________________________
< Plain Text content sent as HTML >
---------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
</pre>
Display of email in Gnome's Evolution showing that the MUA is now displaying the text in Monospace as intended:

-A /home/uid/test.txt
– RubberStamp Apr 04 '19 at 14:29