Sendmail ignoring line endings

Mails from Sendmail are ignoring line endings, when I try to send email with attachment. I have tried to specify the font in the html but line endings are still ignored. I also tried unix2dos, still no luck.

#!/usr/bin/ksh
###Send Email
MAILTO=`cat mail2.list | tr -s '\n' ','`
SUBJECT="bla bla bla"
SENDER=$(nawk -F= 'tolower($0) ~ /emailsender/{print $2;exit;}' param.txt)
ATTACH="filename.txt"
(
 echo "Subject: $SUBJECT"
 echo "From: $SENDER"
 echo "To: $MAILTO"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 echo '<HTML><BODY><PRE>'
 cat report.txt
 echo '</PRE></BODY></HTML>'
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail -t

I can send with monospace font and all lines ending preserved using this (without attachment):

#!/usr/bin/ksh
###Send Email
MAILTO=`cat mail2.list | tr -s '\n' ','`
SUBJECT="bla bla bla"
SENDER=$(nawk -F= 'tolower($0) ~ /emailsender/{print $2;exit;}' param.txt)
(
 echo "Subject: $SUBJECT"
 echo "From: $SENDER"
 echo "To: $MAILTO"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 echo '<HTML><BODY><PRE>'
 cat report.txt
 echo '</PRE></BODY></HTML>'
) | /usr/sbin/sendmail -t

You are falling into the same trap as mohtasims (in whose thread you have answered): you confuse the CONTENT of a file and its REPRESENTATION.

You construct a stream of characters forming your email - by consecutive echo -statements producing some output. If you redirect this into a file and look at the file (by looking at the output of)/with vi , cat , or whatever command is able to produce output you are seeing line breaks at the expected places.

This is because they are there: in fact a file looking like that in display:

ab
cd

will consist of a series of 5 characters: 'a', 'b', '\n', 'c', and 'd' (and probably a another '\n' and maybe a file-end marker which are not relevant to my argument). That it is displayed as two lines consisting of two characters each is because all the programs you used to display the file - vi , grep , cat or what else - interpret the '\n' as: ahh, here i have to start a new line and the next character go in this new line at the leftmost position instead of right to the last one. Without this convention the '\n' would just be ignored and you would see something like:

abcd

or maybe

ab cd

And exactly the same (as the mentioned vi , cat , grep et al.) does sendmail , as you can see if you examine the mail spool file i.e. in /var/spool/mail/* .

The only one NOT adhering to this convention is perhaps your mail reader program (more correctly: mail user agent), which is probably Microsoft Outlook or similar crap. Because against all convention, laid down in RFCs, M$$ once decreed that email is not any longer plain ASCII text by default with MIME-parts in HTML being allowed, but in fact email is HTML from the start (to be precise: not exactly HTML, but what M$$ took as being being HTML, which wasn't quite up to the standard either).

In HTML a line feed is just another space and the above 5-character sequence will be displayed as:

ab cd

If you want to have the same display as in plain text you will have to write this content:

ab<br />cd

or this, it doesn't matter:

ab<br />
cd

because what is '\n' in plain text is '<br />' in HTML. Modify your script like this:

[...]
echo '<HTML><BODY>'
cat report.txt | sed 's/$/<br \/>/'
echo '</BODY></HTML>'
[...]

And you will have your line breaks.

I hope this helps.

bakunin

3 Likes

I would add that in older days, knowing MS Exchange server would mess up the content or display error messages about non compatible characters, I used a .mailrc and with the content of the file:

# =======================================================#
set crt=21
set encoding=8bit
set charset=iso-8859-1
# set mimeheader=yes

# =======================================================#

it solved the issue, but you here, are using HTML tags so not quite the same...

Thank you for the solution, problem solved.