Plain text file pipelined to Linux mailx turns to “Content-Type: application/octet-stream” (an attachment)

Posted on In QA

Plain text file pipelined to Linux mailx turns to “Content-Type: application/octet-stream” which is recognized as an attachment by some email client. The command is like this:

$ cat log.txt | mail -s "Updated log file" -r "from@example.com" "to@example.com"

I expect it to be:

Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

But it turns out to be:

Content-Type: application/octet-stream
Content-Transfer-Encoding: base64

And from the email client (Gmail, thunderbird, etc), it is recognized as an attachment.

How can I force the content to be inlined in the mail as “Content-Type: text/plain”?

From the “MIME types” of the man page of mailx:

If there is a match with the extension of the file to attach, the
given type/subtype pair is used. Otherwise, or if the filename has no
extension, the content types text/plain or application/octet-stream
are used, the first for text or international text files, the second
for any file that contains formatting characters other than newlines
and horizontal tabulators.




If the log.txt file contains formatting characters other than newlines and horizontal tabulators, it’s type will be recognized by mailx as “application/octet-stream”.

Again, from the man page of cat:

-v, –show-nonprinting use ^ and M- notation, except for LFD and TAB

The trick is to add -v as cat‘s option to “show” the nonprinting characters as this:

cat -v log.txt | mail -s "Updated log file" -r "from@example.com" "to@example.com"

This works well for my testing.

Leave a Reply

Your email address will not be published. Required fields are marked *