在Linux系统中发送邮件到外部邮箱(*@qq.com *@hotmail.com)
系统管理人员经常会遇到对于设备或者任务的预警与通知,而且大部分出现这些问题的时间是不可预期的,但是,作为管理人员则需要及时知道这些问题,从而将问题解决。一般情况,可以有发送短信、邮件等方式。发送短信一般来说需要有短信猫(硬件)或者调用libfetion给飞信用户发送。在本文介绍几种简单的发送邮件的方式。
第一种:
sendmail
用法1:echo "内容" | sendmail -oi *@qq.com
用法2:
#!/bin/bash
- /usr/sbin/sendmail -t << EOF
- From: Mail test
- Sender: jkjl
- To: *@qq.com
- Cc: test@g.cn
- Subject: mail testing
- ----------------------------------
- 内容... ...
- ---------------------------------
- EOF
- 保存文件名为sendmail.sh,再运行sh sendmail.sh
- 第二种:
- 用法1:mail -s "title" *@qq.com < 文件名
- 用法2:mail -s "title" -t *@qq.com -t *@qq.com < 文件名
- 用法3:echo "内容" | 用法1或用法2 但无"< 文件名"
- 例如:
- mail -s "TEST" test@163.com < hello.txt
- mail -s "TEST" -t test@163.com -t test@126.com < hello.txt
- echo "Hello" | mail -s "TEST" test@163.com
- 加附件:
- 首先要确定系统已经安装了uuencode模块,而该模块在harutils包中。如没有,可先安装,方法如下:
- sudo apt-get install sharutils
- 或 yum install sharutils
- 用法:uuencode 附件名 显示附件名| mail -s "title" *@qq.com
- 例如:
- uuencode hello.txt bienvenu |mail -s Test test@163.com
- 第三种:
- formail和sendmail共同发邮件
- formail可以封装邮件信息,然后调用sendmail发送,经典例子如下:
- 例如:
- echo hello|formail -I "From:*@163.com" -I "MIME-Version:1.0" -I "Content-type:text/html;charset=gb2312" -I "Subject:test"|sendmail -oi *@qq.com
- 第四种:
- mutt
- 用法:mutt -s "title" test@163.com -a 附件 < 文件名
其中:s—主题 a—附件 - 例如:
- mutt -s "test" *@qq.com -a *.jpg < hello.txt
最后追加的是邮件内容 mutt甚至可以使用pgp加密,利用mutt支持MIME,解决乱码问题等,总之mutt是个人认为的最好用的email工具。