linux每部主机的每个用户都有一个mailbox,我们可以使用mail命令收发邮件。mailbox地址保存于环境变量MAIL中,一般为/var/spool/mail/USERNAME。centos默认会安装并启动邮件服务,仅服务于本机上的用户。
一、邮件发送
[echo "邮件内容" | ]mail [-s "邮件主题"] [-c] username[@localhost] [< /path/to/somefile]
使用方式:
1、mail [-s "邮件主题"] username #在交互模式中输入邮件内容,按Ctrl+D或【.】键提交,连按再次Ctrl+C可中断输入
2、echo "邮件内容" | mail [-s "邮件主题"] username #通过管道输入邮件内容
3、mail [-s "邮件主题"] username < /path/to/somefile #使用重定向从文件中导入内容
4、-c选项表示发送给多个用户,用户之间以空格隔开
查看邮件是否已发出:sendmail -bp,显示结果为"Mail queue is empty"就表示已发出
[root@localhost ~]# mail -c tesla wittgenstein Subject: greeting hello,beauty EOT [root@localhost ~]# mail -s "praise" tesla you are special EOT [root@localhost ~]# mail -s "initial" tesla < /etc/inittab [root@localhost ~]# echo "how are you" | mail -s "visit" tesla [root@localhost ~]# sendmail -bp Mail queue is empty
二、邮件查看与处理
直接使用mail命令可查看邮件,如下
[tesla@localhost ~]$ mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/tesla": 3 messages 3 new U 1 root Wed Oct 7 21:19 19/625 "greeting" U 2 root Wed Oct 7 21:22 19/626 "praise" U 3 root Wed Oct 7 21:24 44/1495 "initial" >N 4 root Wed Oct 7 21:26 18/611 "visit" &
1、U(unread)表示未读邮件,N表示新邮件,后面的数字为邮件编号
2、在&符号后输入邮件编号可查看指定邮件内容,输入help可查看&后可输入的命令
3、&后常用的命令有:
回车键:查看当前邮件,即箭头所指邮件
#:查看第#号邮件
d(delete) 邮件列表:删除指定邮件,可一次性删除多个邮件,邮件编号以空格隔开
R(Reply) 邮件编号:回复发件人
r(reply) 邮件编号:回复发件人和其他收件人
x:退出,不保存之前的操作,例如删除邮件
q:退出,保存之前的操作
& 1 # 查看1号邮件 Message 1: From root@localhost.localdomain Wed Oct 7 21:19:34 2015 ... Status: RO hello,beauty & help #查看帮助 mail commands type <message list> type messages next goto and type next message ... quit quit and apply changes to folder xit quit and discard changes made to folder ! shell escape cd <directory> chdir to directory or home if none given list list names of all available commands A <message list> consists of integers, ranges of same, or other criteria separated by spaces. If omitted, mail uses the last message typed. & d 3 # 删除3号邮件 & r 2 # 回复2号邮件 To: root@localhost.localdomain tesla@localhost.localdomain Subject: Re: praise root@localhost.localdomain (root) wrote: > you are special Thank you,you are charm EOT & q # 退出