1. 程式人生 > >關於linux mail和sendmail的研究與學習

關於linux mail和sendmail的研究與學習

       作為系統與使用者的互動工具,mail有著很重要的作用,系統或者程式可以把一些關鍵事件或訊息放在mail中,供使用者檢視。作為一個linux指令碼開發人員和系統維護人員,瞭解mail命令和使用sendmail傳送郵件是必不缺少的前提。

1、mail檢視郵件

 命令列輸入mail

[[email protected] ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/boco": 2 messages 2 new
>N  1 [email protected]   Fri Feb 14 09:02  18/600   "hello boco"
 N  2 
[email protected]
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鍵中斷工作

3、傳送外部郵件

 mail -s "fff" [email protected]
 這種是用當前使用者名稱@主機名 作為使用者名稱進行傳送的。

 因為mail的還得靠sendmail來投遞郵件,根據網上資料可以這樣檢視sendmail是否有郵件待發,如果沒有,說明郵件全都發送出去了。(當然你的sendmail服務要開著

 [[email protected] ~]# sendmail -bp
 /var/spool/mqueue is empty
                    Total requests: 0
 [[email protected] sendEmail-v1.56]# ./sendEmail -f [email protected] -t [email protected] -u "from sendmail" -xu [email protected] -xp ××××(密碼) -m happy3 -s smtp.163.com
 Feb 14 13:15:19 redhat5 sendEmail[684]: Email was sent successfully!
 [[email protected] 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 [email protected] –t [email protected] –s smtp.163.com –u "subject” –xu sender –xp 123456 –m happy