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

时间秒杀一切 提交于 2019-11-30 08:45:48

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: person@example.com
      echo From: you@example.com
      echo Subject: a logfile
      echo
      cat logfile.log
    ) | sendmail -t
    

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" "person@example.com"

More details of the discussion of cat with mailx.

I had some trouble to get my automatic email scripts to run after changing to Ubuntu Precise 12.04. I don't know, when Ubuntu (or Debian) exchanged bsd-mailx against heirloom-mailx, but the two "mail"-commands behave very differently. (E.g. heirloom uses -a for attachments, while it's used for additional headers in bsd.) In my case heirloom-mailx wasn't able to reliably determine the Mime type and kept sending text as attachments. Blame me for not weeding out control characters or whatever, but I don't see much point in changing scripts that did their job perfectly before the upgrade. So if you prefer setting the Mimetype yourself, bsd-mailx is a better solution.

sudo apt-get install bsd-mailx 
sudo apt-get remove heirloom-mailx

Solved it for me.

On RedHat based systems (SL, CentOS, Fedora, etc.), you will want to install bsd-mailx and then set /etc/alternatives/mail appropriately:

sudo yum -y install bsd-mailx
sudo alternatives --set mail /usr/bin/bsd-mailx

Of course, you risk breaking applications that rely on heirloom-mailx behavior but do not explicitly call 'mailx' instead of 'mail'.

To display information about /bin/mail currently points to:

sudo alternatives --display mail

To check for various installed mailx packages:

sudo rpm -qa *mailx

In my case, the script was called from cron where LC_* was not defined and accents were interpreted as "control chars". I just inserted the following lines at the beginning of my crontab file :

LC_NAME=fr_FR.UTF-8
LC_ALL=fr_FR.UTF-8
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!