Linux shell mail add attachment(Linux用shell发邮件带附件)

    前段时间国庆放假,积累了好几个report没有发,以前都是report程序跑出csv,然后我再手工发给customer,因为放长假的原因就推迟了发送,今天想想不如设置为生成report之后就自动发给customer算了,虽然可能格式丑一点,但和customer沟通过没问题。这样我也可以省点时间了。

    之前发邮件一直是用sendmail,但查来查去好像不支持带附件,网上到处都是用uuencode + mail来发附件的例子,那便用这个吧。

    邮件正文写在一个txt文件里,再用uuencode发附件,其实很简单,一个命令就搞定了:

# ( cat mailbody.txt; uuencode $filename $reportname ) | mail -s "mail subject" sendto@xxx.xxx

    mailbody是邮件正文,要发送的文件为$filename,但显示在邮件附件里的名称叫$reportname。就这么简单。但真省了不少事。

    BTW: 如果不能运行uuencode,需要安装sharutils

# yum install sharutils 

参考:
http://www.cnblogs.com/lidp/archive/2010/01/03/1697362.html
http://www.webjx.com/server/linux-2724.html
网上还有另外一种方法,目前没有使用:
http://blog.myhnet.cn/2009/11/12/how-to-send-email-with-attachment-in-linux-text-terminal/

posted @ 2010-10-14 08:51  lettoo  阅读(3079)  评论(0编辑  收藏  举报