Linux mail命令:发送和接收电子邮件

mail 是 Linux 的邮件客户端命令,能够利用这个命令给其余用户发送邮件。web

mail 命令的基本信息以下。
命令名称:mail。
英文原意:send and receive Internet mail。
所在路径:/bin/mail。
执行权限:全部用户。
功能描述:发送和接收电子邮件。shell

【例 1】发送邮件。
若是咱们想要给其余用户发送邮件,则能够执行以下命令:
[root@localhost ~]# mail userl
Subject: hello <-邮件标题
Nice to meet you! <-邮件具体内容
. <-使用.来结束邮件输入
#发送邮件给user1用户app

咱们接收到的邮件都保存在"/var/spod/mail/用户名"中,每一个用户都有一个以本身的用户名命名的邮箱。编辑器

【例 2】发送文件内容。
若是咱们想把某个文件的内容发送给指定用户,则可执行以下命令:
[root@localhost ~]# mail -s “test mail” root </root/ anaconda-ks.cfg
#把/root/anaconda-ks.cfg文件的内容发送给root用户svg

选项:
-s: 指定邮件标题ui

咱们在写脚本时,有时须要脚本自动发送一些信息给指定用户,把要发送的信息预先写到文件中,是一个很是不错的选择。命令行

【例 3】查看已经接收的邮件。
咱们能够直接在命令行中执行 mail 命令,进入 mail 的交互命令中,能够在这里査看已经接收到的邮件。例如:
[root@localhost ~]# mail
Heirloom Mail version 12.4 7/29/08.Type ?for help.
“/var/spool/mail/root”: 1 message 1 newxml

N 1 root Mon Dec 5 22:45 68/1777 “test mail”<-以前收到的由件
N 2 root Mon Dec 5 23:08 18/602 “hello”
#未阅读编号发件人 时间 标题
&
<-等待用户输入命令ip

能够看到已经接收到的邮件列表,"N"表明未读邮件,若是是已经阅读过的邮件,则前面是不会有这个"N"的;以后的数字是邮件的编号,咱们主要经过这个编号来进行邮件的操做。如餓们想要査看第1邮件,则只需输入邮件的编号"1"就能够了》ci

在交互命令中执行"?",能够查看这个交互界面支持的命令。例如:
&? <-输入命令
mail commands
type type messages
next goto and type next message
from give head lines of messages
headers print out active message headers
delete delete messages
undelete undelete messages
save folder append messages to folder and mark as saved
copy folder append messages to folder without marking them
write file append message texts to file, save attachments
preserve keep incoming messages in mailbox even if saved
Reply reply to message senders
reply reply to message senders and all recipients
mail addresses mail to specific recipients
file folder change to another folder
quit quit and apply changes to folder
xit quit and discard changes made to folder
! shell escape
cd chdir to directory or home if none given
list list names of all available commands

这些交互命令是能够简化输入的,好比"headers"命令,就能够直接输入"h",这是列出邮件标题列表的命令。咱们解释一下经常使用的交互命令。 headers:列出邮件标题列表,直接输入"h"命令便可。 delete:删除指定邮件。好比想要删除第二封邮件,能够输入"d2"。 save:保存邮件。能够把指定邮件保存成文件,如"s 2/tmp/test.mair。 quit:退出,并把已经操做过的邮件进行保存。好比移除已删除邮件,保存已阅读邮脾。 exit:退出,可是不保存任何操做。 < Linux write命令Vim文本编辑器 >