.dat attachment instead of text using mailx in Red

2019-05-08 18:30发布

问题:

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 me@email.ca

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.

回答1:

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' me@email.ca; should work correctly.



回答2:

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


回答3:

As per Toru's answer, I've been settled down to this command:

cat $FILE | tr -c -d '[:graph:][:blank:]\n\r\t' | mail -s "$FILE" somebody@example.com