Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux mail < file.log has Content-Type: application/octet-stream (a noname attachment in Gmail)

Tags:

linux

email

mailx

I've been using

 mail -s "here is a log file" "[email protected]" < log/logfile.log

Which used to come through with headers:

User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

But now the files are longer I'm getting noname attachments because with this:

User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64

So if all else fails, check the manual man mail ...

NAME
       mailx - send and receive Internet mail

SYNOPSIS
       mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops]
              [-A account] [-S variable[=value]] to-addr . . .

None of these options seem useful so how can I force Content-Type: text/plain?

like image 435
KCD Avatar asked Apr 27 '12 00:04

KCD


2 Answers

The man page is a good place to start! Keep reading until you get to the MIME TYPES section, and pay close attention the following:

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 char‐ acters other than newlines and horizontal tabulators.

So, if your message contains "formatting characters" (which in general means control characters) other than newlines and tabs, it will automatically be classified as application/octet-stream. I bet that if you look closely at the data you'll find some control characters floating around.

You can work around this by...

  • Including the log file as an attachment (using -a) instead of the main message body, and set up your ~/.mime.types file to identify *.log files as text/plain.
  • Filter out control characters using something like tr.
  • Use another MUA such as mutt to send the mail. In fact, you could just craft a message yourself and send it directly to sendmail:

    (
      echo To: [email protected]
      echo From: [email protected]
      echo Subject: a logfile
      echo
      cat logfile.log
    ) | sendmail -t
    
like image 92
larsks Avatar answered Sep 23 '22 03:09

larsks


I got the similar problem recently and finally end up with a solution that is shorter:

cat -v log/logfile.log | mail -s "here is a log file" "[email protected]"

More details of the discussion of cat with mailx.

like image 31
ericzma Avatar answered Sep 22 '22 03:09

ericzma