linux mail < file.log 具有 Content-Type: application/octet-stream(Gmail 中的无名附件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10343106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:01:13  来源:igfitidea点击:

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

linuxemailmailx

提问by KCD

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:

但是现在文件更长了,我收到了 noname 附件,因为这样:

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...

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

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

采纳答案by larsks

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

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

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.

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

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.

因此,如果您的消息包含除换行符和制表符以外的“格式化字符”(通常表示控制字符),它将自动归类为application/octet-stream. 我敢打赌,如果您仔细查看数据,您会发现一些控制字符四处飘散。

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.typesfile to identify *.logfiles as text/plain.
  • Filter out control characters using something like tr.
  • Use another MUA such as muttto 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
    
  • 包括日志文件作为附件(使用-a)而不是主要消息正文,并设置您的~/.mime.types文件以将*.log文件标识为文本/纯文本。
  • 使用类似tr.
  • 使用另一个 MUA,例如mutt发送邮件。事实上,您可以自己制作一条消息并将其直接发送到sendmail

    (
      echo To: [email protected]
      echo From: [email protected]
      echo Subject: a logfile
      echo
      cat logfile.log
    ) | sendmail -t
    

回答by JC Boggio

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 :

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

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

回答by some-non-descript-user

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.

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

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

Solved it for me.

为我解决了。

回答by ericzma

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.

有关cat 与 mailx讨论的更多详细信息。

回答by user3759925

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

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

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'.

当然,您可能会破坏依赖于 heirloom-mailx 行为但没有明确调用“mailx”而不是“mail”的应用程序。

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

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

sudo alternatives --display mail

To check for various installed mailx packages:

要检查各种已安装的 mailx 软件包:

sudo rpm -qa *mailx