Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.dat attachment instead of text using mailx in RedHat Linux

I am trying to send the contents of a text file as the body of an attachment. This works fine in HP-UX, but we've recently moved to RedHat Linux, and it is no longer working as expected.

Here's my command

cat test.txt | mailx -sTest [email protected]

If "test.txt" contains low ASCII characters, then it works fine. However, my text file may have French characters and will always contain a registered trademark symbol. It seems that when I try to send those characters, Linux is converting the email into an attachment (in the form attxxxxx.dat). The attachment has all my data, perfectly formed, but my recipients just want a plain email - not a "dat" attachment. We've tried setting the environment variables and putting extended character set commands in the mailx command, to no avail.

Any suggestions or ideas would be greatly appreciated.

like image 541
David Reid Avatar asked Oct 21 '22 01:10

David Reid


2 Answers

Make sure that your file encoding is the same as the locale (man locale) set on your system. Either convert your file (e.g. using iconv) to the according locale or set the locale of the system to the current file encoding. Moreover make sure to remove any carriage returns from the file

cat test_1.txt | tr -d '\r' > test_2.txt;

Then cat test_2.txt | mailx -s 'Test' [email protected]; should work correctly.

like image 114
Toru Avatar answered Oct 24 '22 03:10

Toru


you can also use below small perl script before you send the file via mail command

perl -pi -e 's/\r\n/\n/g' file_name
like image 25
Kapil Avatar answered Oct 24 '22 02:10

Kapil