作為系統與用戶的交互工具,mail有著很重要的作用,系統或者程序可以把一些關鍵事件或消息放在mail中,供用戶查看。作為一個linux腳本開發人員和系統維護人員,了解mail命令和使用sendmail發送郵件是必不缺少的前提。
1、mail查看郵件命令行輸入mail
[boco@redhat5 ~]$ mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/boco": 2 messages 2 new
>N 1 root@redhat5.4-baby Fri Feb 14 09:02 18/600 "hello boco"
N 2 root@redhat5.4-baby Fri Feb 14 11:13 17/567
& ?
Mail Commands
t <message list> type messages 顯示指定郵件
n goto and type next message
e <message list> edit messages
f <message list> give head lines of messages
d <message list> delete messages 刪除指定郵件,可以使用d 2-100 刪除第2到100條郵件
s <message list> file append messages to file 寫到文件中
u <message list> undelete messages
R <message list> reply to message senders
r <message list> reply to message senders and all recipients
pre <message list> make messages go back to /usr/spool/mail
m <user list> mail to specific users
q quit, saving unresolved messages in mbox 保存已看過的郵件到~/mbox 中,若執行pre ,則不保存
x quit, do not remove system mailbox 效果好像沒有看過似得
h print out active message headers 顯示郵件列表
! shell escape 執行命令
cd [directory] chdir to directory or home if none given
A <message list> consists of integers, ranges of same, or user names separated
by spaces. If omitted, Mail uses the last message typed.
A <user list> consists of user names or aliases separated by spaces.
Aliases are defined in .mailrc in your home directory.
&
如果郵件被收取放到 ~/mbox 可以這樣查看它們:
# mail -f ~/mbox
2、發送內部郵件
mail -s "hi, boco, i'm root" boco < 郵件內容
或者
mail -s "hi, boco, i'm root" snail
plz reply me, snail.
.
Cc:
【注意】使用.或者Ctrl+D或者 . 來結束內容輸入
【注意】默認情況下連按兩次Ctrl+C鍵中斷工作
mail -s "fff" god_is_coming@163.com
這種是用當前用戶名@主機名 作為用戶名進行發送的。
因為mail的還得靠sendmail來投遞郵件,根據網上資料可以這樣查看sendmail是否有郵件待發,如果沒有,說明郵件全都發送出去了。(當然你的sendmail服務要開著)
[root@pps ~]# sendmail -bp
/var/spool/mqueue is empty
Total requests: 0
網上說用sendmail 可以實現用特定用戶名來發送外部郵件,但我在redhat 5.4上面沒有試成功。但用下面的方法就可以實現。
下載新的sendEmail
sendEmail的主頁http://caspian.dotconf.net/menu/Software/SendEmail/
下載地址http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
解壓,運行
#tar –zxvf sendEmail-v1.56.tar.gz
#cd sendEmail-v1.56
然後給確認確實它具有執行權限
[root@redhat5 sendEmail-v1.56]# ./sendEmail -f god_is_coming@163.com -t weichaofan@boco.com.cn -u "from sendmail" -xu god_is_coming@163.com -xp ××××(密碼) -m happy3 -s smtp.163.com Feb 14 13:15:19 redhat5 sendEmail[684]: Email was sent successfully! [root@redhat5 sendEmail-v1.56]# 解釋: -f 表示發送者的郵箱 -t 表示接收者的郵箱 -s 表示SMTP服務器的域名或者ip -u 表示郵件的主題 -xu 表示SMTP驗證的用戶名 -xp 表示SMTP驗證的密碼(不能有特殊字符喲) -m 表示郵件的內容
如果你不帶-m參數的話,就會提示你自行輸入
Reading message body from STDIN because the ‘-m’ option was not used.
If you are manually typing in a message:
- First line must be received within 60 seconds.
- End manual input with a CTRL-D on its own line
輸入完成後使用CTRL-D來結束
當然我們也可以將一個文件的內容作為郵件的正文發出去的
那麼就可以使用:
cat 文件名 | /usr/local/bin/sendEmail –f sender@163.com –t receiver@163.com –s smtp.163.com –u "subject” –xu sender –xp 123456 –m happy