linux邮件< file.log有Content-Type:application / octet-stream(Gmail中的noname附件)

时间:2012-04-27 00:12:32

标签: linux email mailx

我一直在使用

 mail -s "here is a log file" "person@example.com" < log/logfile.log

以前用过的标题:

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

但是现在文件越来越长我得到了noname附件因为这个:

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

因此,如果所有其他方法都失败了,请查看手册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 . . .

这些选项似乎都没用,所以如何强制Content-Type: text/plain

5 个答案:

答案 0 :(得分:11)

手册页是一个很好的起点!继续阅读,直至到达MIME TYPES部分,并密切关注以下内容:

  

否则,或者如果文件名没有扩展名,则为内容类型   text / plain或application / octet-stream是          使用,第一个用于文本或国际文本文件,第二个用于包含格式化字符的任何文件          除了换行符和水平制表符之外的其他内容。

因此,如果您的邮件包含除换行符和制表符以外的“格式化字符”(通常表示控制字符),它将自动归类为application/octet-stream。我敢打赌,如果仔细观察数据,你会发现一些控制字符浮动。

你可以通过...来解决这个问题。

  • 将日志文件包含为附件(使用-a)而不是主消息正文,并设置~/.mime.types文件以将*.log文件标识为text / plain。
  • 使用tr
  • 之类的内容过滤掉控制字符
  • 使用mutt等其他MUA发送邮件。实际上,您可以自己制作一条消息并将其直接发送到sendmail

    (
      echo To: person@example.com
      echo From: you@example.com
      echo Subject: a logfile
      echo
      cat logfile.log
    ) | sendmail -t
    

答案 1 :(得分:8)

我最近遇到了类似的问题,最终得到了一个更短的解决方案:

cat -v log/logfile.log | mail -s "here is a log file" "person@example.com"

discussion of cat with mailx

的更多细节

答案 2 :(得分:3)

在更改为Ubuntu Precise 12.04后,让我的自动电子邮件脚本运行时遇到了一些麻烦。我不知道,当Ubuntu(或Debian)与heirloom-mailx交换bsd-mailx时,两个“邮件”命令的行为却截然不同。 (例如,传家宝使用-a作为附件,而它用于bsd中的其他标题。) 在我的情况下,heirloom-mailx无法可靠地确定Mime类型并继续将文本作为附件发送。怪我没有除去控制字符或其他什么,但我没有看到更改脚本在升级之前完成工作的重点。 因此,如果您更喜欢自己设置Mimetype,那么bsd-mailx是更好的解决方案。

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

为我解决了。

答案 3 :(得分:3)

在基于RedHat的系统(SL,CentOS,Fedora等)上,您需要安装bsd-mailx,然后适当地设置/ etc / alternatives / mail:

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

当然,您冒着破坏依赖传家宝邮件行为的应用程序的风险,但没有明确地打电话给#mail;&#39;而不是&#39; mail&#39;。

显示有关/ bin / mail当前指向的信息:

sudo alternatives --display mail

检查各种已安装的mailx包:

sudo rpm -qa *mailx

答案 4 :(得分:2)

在我的情况下,脚本是从cron调用的,其中LC_ *未定义,重音被解释为“控制字符”。我刚刚在我的crontab文件的开头插入了以下行:

LC_NAME=fr_FR.UTF-8
LC_ALL=fr_FR.UTF-8