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

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

问题描述

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

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

/ * exec.sh * /
#/ bin / sh
cd / opt / bin
./ analysis.sh>测试
mail -s今日天气example@example.com<测试

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

解决方案

自己也是同样的问题, code> mailx - 传家宝邮件12.4 7/29/08



当在命令行上运行脚本时,电子邮件以正常的电子邮件与文本正文。

但是,当我通过 crontab 运行完全相同的脚本时,电子邮件的正文作为附件 - ATT00001。 BIN(Outlook),应用程序/八位字节流(mutt)或noname(Gmail)。



要研究一下, p>

问题



如果在文本输入中遇到未知/控制字符,将其转换为具有应用程序/八位字节流MIME类型集的附件。



从手册页:


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


所以你需要删除这些控制字符,这可以通过ie tr

  echo$ Output| / usr / bin / tr -cd'\11\12\15\40-\176'|邮件... 

然而,由于我有挪威UTF8字符:æøå - 列表展开,而你真的很想维护这样的名单,而我需要挪威人物。



检查附件,我发现我只有\ r,\范围32-176中的常规ASCII字符 - 全部可打印 184和195 - > UTF8



Sollution



在您的脚本中明确设置区域设置:

  LANG =en_US.UTF8;出口LANG 

在您的shell中运行 export setenv 如果您运行 csh tcsh 以确定什么您的地区设置为。



说明



Mailx - 在您的外壳 - LANG设置为.UTF8,将正确识别UTF8字符并继续。



当运行在 crontab LANG未设置,默认为LANG = C,因为默认情况下,crontab将仅运行一组受限制的环境变量(系统依赖)。



mailx(或其他程序)然后不会识别UTF8字符,并确定输入包含未知的控制字符。



我的问题是UTF8字符,您可以是其他控件字符在您的输入。运行它通过 hexdump od -c ,但由于它可以在常规shell中正常运行我怀疑LANG问题



参考文献:




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

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

解决方案

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

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:

Problem

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

From the man page:

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

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

echo "$Output" | /usr/bin/tr -cd '\11\12\15\40-\176' | mail ...

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.

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

Sollution

Explicitly set the locale in your script:

LANG="en_US.UTF8" ; export LANG

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

Explanation

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

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 (or other programs) will then not recognize UTF8 characters and determine that the input containes unknown control characters.

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.

References:

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

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