使用mail命令发送邮件

1、使用shell当编辑器

1
2
3
mail -s "hi,this is a test by shell" [email protected]
hello
welcome to www.cszhi.com

-s后面接邮件的主题,[email protected]是邮件的接收人,输入完这行命令后回车,会进入邮件正文的编写。当邮件正文输入完成后,按CTRL+D结束输入,邮件就发送出去了。

2、使用管道发送邮件

1
echo "hello,welcome to www.cszhi.com" | mail -s "hi,this is a test by pipe" [email protected]

使用管道直接敲入这行命令即可完成邮件的发送,echo后面接的是邮件正文

3、使用文件作为邮件内容

1
mail -s "hi,this is a test by file" [email protected] <content.txt

上面的命令把content.txt文件的内容作为邮件的内容发送给[email protected]了。

因为mail程序本身就是调用sendmail来进行邮件发送的,因此我们可以在mail命令中使用sendmail的参数进行配置,比如我想使用特定的发件人发送邮件,可以使用如下命令:

1
mail -s "this is a test " [email protected] -- -f [email protected] <content.txt

上面的命令中,我们使用了– -f [email protected]这样的参数,这是sendmail的选项,其中-f表示邮件的发送人邮件地址。

4、发送包含附件的邮件
现在的邮箱动辄好几个G,可以接收的附件也越来越大,用来备份一些小型数据还是蛮不错的。
使用mail命令发送带附件的邮件也很简单,不过需要先安装uuencode软件包,这个程序是对二进制文件进行编码使其适合通过邮件进行发送:

1
yum install sharutils

安装完成后我们就可以来进行附件的发送了,使用如下命令:

1
uuencode test.txt test | mail -s "hello,see the attachement" [email protected] <content.txt

完成后就可以把text.txt文件作为邮件的附件发送出去了。uuencode有两个参数,第一个是要发送的文件,第二个是显示的文件名称。

另外,在centos 6.0上的mail已经有一个-a参数,可以直接添加附件:

1
echo "hello,see the test.txt" |mail -a test.txt -s "helo" [email protected]