Sendmail ignoring line endings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sendmail ignoring line endings
# 1  
Old 11-05-2016
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.
Code:
#!/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):
Code:
#!/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

# 2  
Old 11-08-2016
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:

Code:
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:

Code:
abcd

or maybe

Code:
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:

Code:
ab cd

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

Code:
ab<br />cd

or this, it doesn't matter:

Code:
ab<br />
cd

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

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

And you will have your line breaks.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 3  
Old 11-08-2016
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:
Code:
# =======================================================#
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...
# 4  
Old 11-08-2016
Quote:
Originally Posted by bakunin
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:

Code:
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:

Code:
abcd

or maybe

Code:
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:

Code:
ab cd

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

Code:
ab<br />cd

or this, it doesn't matter:

Code:
ab<br />
cd

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

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

And you will have your line breaks.

I hope this helps.

bakunin
Thank you for the solution, problem solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Tip to remove line endings and spaces on a pre-formatted text file?

Hi, At the moment, using Notepad++ to do a search and replace, manually section by section which is real painful. Yeah, so copying each section of the line of text and putting into a file and then search and replace, need at least 3-operations in Notepad++. Here's hoping I will be able to... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

"sed" ignoring last line

Hi, I am giving below command script and getting below output. I tried using "sed" which is ignoring 4th line. Can you please help me to get the expected output like below Code: echo "dis clusqmgr(*) cluster(BT.CL.OFSSTAT4) conname qmtype deftype"| runmqsc -e $QMGR|egrep... (7 Replies)
Discussion started by: darling
7 Replies

3. Red Hat

Sendmail ignoring Mailertable

Hi Friends, I am running sendmail 8.14 on rhel6. I have 2 mail servers as serv1.home.com and test.home.com. Currently test.home.com is pointing as MX for home.com domain. So what I am trying to do is to route emails arriving at test.home.com server to serv1.home.com using mailertable. I have... (0 Replies)
Discussion started by: Rohit Bhanot
0 Replies

4. UNIX for Advanced & Expert Users

vimrc help with line endings

I was reading this and thought I could put this in my vimrc and it would convert the line endings to unix. Am I doing something wrong or am I missing something? set ff=unixManaging/Munging Line-Endings with Vi/Vim | Jeet Sukumaran I used this command and it confirms that my global option is... (2 Replies)
Discussion started by: cokedude
2 Replies

5. UNIX for Dummies Questions & Answers

Find a file that could have different endings

Hello all. Hope you can help. I am looking for a complete command to search for a file named HOSPCHK. The file could be listed with numbers after it like it could be listed with letters after it or a combination of both or just by it self. The other catch is the file that I want to look for... (27 Replies)
Discussion started by: azapp51
27 Replies

6. UNIX for Advanced & Expert Users

line endings help of non-ASCII files

When you are dealing with ASCII files it easy to check on line endings type. You can just use the file command. You are not always lucky enough to be dealing with ASCII files. So in the cases that you don't have ASCII files how can you check what type of line endings you have? Please list all... (5 Replies)
Discussion started by: cokedude
5 Replies

7. UNIX for Advanced & Expert Users

Vi line endings conversions

I was reading these 2 articles. Why does the wikia one think :e ++ff=dos? Or am I just misunderstanding it? :e ++ff=unix :e ++ff=dos File format - Vim Tips Wiki Managing/Munging Line-Endings with Vi/Vim | Jeet Sukumaran (1 Reply)
Discussion started by: cokedude
1 Replies

8. Shell Programming and Scripting

Problems with Sed/awk/grep and line endings

Hello I have created the following script, which is designed to manipulate a text document: #!/bin/sh # Get 3 lines, (last of which is "Quantity"); adjust order; put all three on one line with tabs. FILENAME=~/Desktop/email.txt LIST=$(grep -B2 "Quantity" ${FILENAME} |awk 'BEGIN { FS = "\n"; RS... (6 Replies)
Discussion started by: benwiggy
6 Replies

9. Shell Programming and Scripting

How to extract a string from a file ignoring new line

Hi, sumdays before i had posted a query with same subject. i got sum great help from great ppl which solved my problem then. But now there is a small problem with the code that i need the experts help upon. for parsing a text like this where $ had been the delimiter between... (3 Replies)
Discussion started by: suresh_kb211
3 Replies

10. Shell Programming and Scripting

ignoring blank line in a file

i have a file called Cleaner1.log . This files have some blank lines also.My requirement is that it should ignore the blank lines and give me the lines that contain some data. I m using this logic in a script: below the contents of file : Maximum Time Taken for Processing(Failed) RR... (4 Replies)
Discussion started by: ali560045
4 Replies
Login or Register to Ask a Question