/usr/bin/mail not interpreting new lines

0

I have a strange problem when piping a cat to mail.

I have a simple text file which has been compiled from a script, which is a report on what folders were copied in the script. I then want to send this report via email to the system administrators.

The script generates the report just fine. When I run

cat /tmp/report.txt

I get nicely formatted output with all the line breaks I would expect.

Copy report. Started on Mon Sep 28 13:37:57 BST 2015

Run in progress?: Yes

3 directories were copied.

Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps


Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)
Copy completed on 2015.09.28 at 13:37:59
Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps


Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)
Copy completed on 2015.09.28 at 13:38:01
Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps

However, for some reason when I now pipe this to mail

cat /tmp/report.txt | mail -s "Copy Report" recipient@domain.com

or

mail -s "Copy Report" recipient@domain.com < /tmp/report.txt

This is the email I get.

Copy report. Started on Mon Sep 28 13:37:57 BST 2015

Run in progress?: Yes

3 directories were copied.

Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps


Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14) Copy completed on 2015.09.28 at 13:37:59 Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps


Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18) Copy completed on 2015.09.28 at 13:38:01 Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps

What gives? Some line breaks are there, some aren't! I've tried editing the file in both vi and nano and ensured the line breaks were actually there. Also deleted them and made them again and the email output remains the same.

Using /usr/bin/mail

Further info:

I hand typed the following into a text file and did the same pipe to mail. I put the word "Copy" at the start of each line.

Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
Copy 0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
Copy 0inputs+355896outputs (0major+150minor)pagefaults 0swaps

This is what I got in the email:

Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps

I then removed all of the "Copy" words. This is what I got in the email.

Started on 2015.09.29 at 09:46:49
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Completed on 2015.09.29 at 09:46:49
Info:
0.00user 0.46system 0:01.98elapsed 23%CPU (0avgtext+0avgdata 2552maxresident)k
0inputs+355896outputs (0major+149minor)pagefaults 0swaps

Thought it might be something to do with the word Copy but I've still got one line with no line break!

This is the output of cat -vet /tmp/report.txt

Copy report. Started on Mon Sep 28 13:37:57 BST 2015$
$
Run in progress?: Yes$
$
$
$
3 directories were copied.$
$
$
Copy started on 2015.09.28 at 13:37:57$
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)$
Copy completed on 2015.09.28 at 13:37:57$
Copy info:$
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k$
0inputs+355896outputs (0major+150minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:37:59$
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)$
Copy completed on 2015.09.28 at 13:37:59$
Copy info:$
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k$
0inputs+355896outputs (0major+152minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:38:01$
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)$
Copy completed on 2015.09.28 at 13:38:01$
Copy info:$
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k$
0inputs+355896outputs (0major+151minor)pagefaults 0swaps$

Andy Foster

Posted 2015-09-28T13:47:42.623

Reputation: 311

Are you using CRLF for line breaks in the email as per RFC 5322 section 2.1?

– Andrew Morton – 2015-09-29T17:32:57.247

What linux version do you run? What version of /usr/bin/mail are you running? Is it a redirect to something else like Ubuntu has it redirected to hairloom-mailx? – ojs – 2015-09-29T18:17:12.113

Answers

2

You are probably suffering from the "format=flowed" feature introduced a long time ago, see rfc2646.

You will probably find some lines have a trailing space character. This is interpreted by some mail readers as meaning "concatenate this line with the next and then redo the text wraparound for line lines to fit in the screen width".

Normally, this should only be done if your mail has the Content-Type header option format=flowed, but nowadays, due to the non-Unix crowd, this has become a default.

Use cat -vet on your data to see if there are trailing spaces. Use sed 's/ \+$//' to remove them.


I didn't see any trailing spaces nor control characters not carriage-returns in your data, so there's no real excuse for your mailer tampering with it.

One last try: as Andrzej A. Filip suggested, instead of mail go straight to sendmail. This will allow you to add an explicit content-type header. Eg:

(echo 'Subject: Copy Report'
 echo 'Content-Type: text/plain; charset=us-ascii; format=fixed'
 echo
 cat /tmp/report.txt
) |  sendmail recipient@domain.com

The last echo is to end the headers before the data. sendmail may not be in your PATH, look in /sbin and /usr/sbin and so on.

meuh

Posted 2015-09-28T13:47:42.623

Reputation: 4 273

Another method is use /usr/sbin/sendmail directly - it would avoid adding headers by mail program. – AnFi – 2015-09-28T15:18:22.337

Thanks for your suggestions but unfortunately neither solution works. cat -vet shows that it is formatted correctly and there are no trailing spaces. Using sendmail gives me the same result. – Andy Foster – 2015-09-28T15:42:03.783

did cat -vet show any control chars like ^M or ^[ ? – meuh – 2015-09-28T15:47:02.317

I added an attempt to use sendmail directly. – meuh – 2015-09-29T14:44:56.380