使用 crontab 作业发送邮件,邮件正文变成了一个名为 ATT00001.bin 的附件 [英] Use crontab job send mail, The email text turns to an attached file which named ATT00001.bin

查看:147
本文介绍了使用 crontab 作业发送邮件,邮件正文变成了一个名为 ATT00001.bin 的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一台 linux 服务器中分析一些数据,然后将它作为电子邮件文本发送到我的电子邮件帐户,但是当我在 shell 命令中执行这个 shell 脚本时,它运行良好,奇怪的是当我把所有的程序进入 crontab 作业,电子邮件文本将变成附件,有人可以帮忙吗?

I want to analysis some data in one linux server,then send the it as Email text to my Email account , But when i execute this shell scripts in shell command, It works well, Weird is that when i put all the procedure into crontab job, The Email text will turns to an attached file, Can someone help?

#* * * * * sh -x /opt/bin/exec.sh >> /opt/bin/mailerror 2>&1

/* exec.sh */
#/bin/sh
cd /opt/bin
./analysis.sh > test
mail -s "Today's Weather" example@example.com < test

但是当我直接在 shell 命令行中执行 exec.sh 时,电子邮件会收到文本,有人可以为我解释一下,谢谢.

But when i execute exec.sh in shell command line directly, The Email will get text, Can someone explain it for me, grate thanks.

推荐答案

我自己也遇到了同样的问题,只是我将文本输出传送到 mailx - Heirloom mailx 12.4 7/29/08

Ran into the same problem myself, only I'm piping text output into mailx - Heirloom mailx 12.4 7/29/08

在命令行上运行脚本时,电子邮件作为带有文本正文的普通电子邮件出现.
但是,当我通过 crontab 运行完全相同的脚本时,电子邮件的正文以附件形式出现 - ATT00001.BIN (Outlook)、application/octet-stream (mutt) 或 "noname" (Gmail).

When running the script on the command line the email came out as normal email with a text body.
However, when I ran the exact same script via crontab the body of the email came as an attachment - ATT00001.BIN (Outlook), application/octet-stream (mutt) or "noname" (Gmail).

进行了一些研究来解决这个问题,但这里是:

Took some research to figure this out, but here goes:

问题

Mailx 会在文本输入中遇到未知/控制字符时,将其转换为带有 application/octet-stream mime-type 集的附件.

Mailx will, if it encounters unknown / control characters in text input, convert it into an attachment with application/octet-stream mime-type set.

来自手册页:

对于包含除换行符和水平制表符以外的格式字符的任何文件

for any file that contains formatting characters other than newlines and horizontal tabulators

所以你需要删除那些控制字符,这可以用 tr

So you need to remove those control characters, which can be done with i.e. tr

echo "$Output" | /usr/bin/tr -cd '11121540-176' | mail ...

但是,因为我有挪威语 UTF8 字符:æøå - 列表扩展了,你真的不想维护这样的列表,我需要挪威语字符.

However since I had Norwegian UTF8 characters: æøå - the list expand, and you don't really want to maintain such a list, and I need the norwegian characters.

并检查附件,我发现我只有 , 范围 32-176 的常规"ASCII 字符 - 所有可打印的 184 和 195 --> UTF8

And inspecting the attachment I found I had only , the "regular" ASCII characters in range 32-176 - all printable and 184 and 195 --> UTF8

解决方案

在脚本中显式设置语言环境:

Explicitly set the locale in your script:

LANG="en_US.UTF8" ; export LANG

在你的 shell 中运行 export - 或者 setenv 如果你运行 cshtcsh 来确定你的语言环境设置为.

Run export in your shell - or setenv if you run csh or tcsh to determine what your locale is set to.

说明

Mailx - 在 shell 中运行时 - 将 LANG 设置为 .UTF8,将正确识别 UTF8 字符并继续.

Mailx - when run in your shell - with LANG set to .UTF8, will correctly identify the UTF8 chars and continue.

当在 crontab 中运行时未设置 LANG,默认为 LANG=C,因为默认情况下 crontab 将仅运行一组受限的环境变量(取决于系统).

When run in crontab LANG is not set, and default to LANG=C, since by default crontab will run only a restricted set of environment variables (system dependant).

mailx(或其他程序)将识别 UTF8 字符并确定输入包含未知的控制字符.

mailx (or other programs) will then not recognize UTF8 characters and determine that the input containes unknown control characters.

我的问题是 UTF8 字符,您的问题可能是输入中的其他控制字符.通过 hexdumpod -c 运行它,但由于它在常规 shell 中工作正常,我怀疑 LANG 问题.

My issue was UTF8 characters, yours could be other control characters in your input. Run it through hexdump or od -c, but since it works OK in a regular shell I'm suspecting LANG issues.

参考:

这篇关于使用 crontab 作业发送邮件,邮件正文变成了一个名为 ATT00001.bin 的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆