Linux下使用mail發送郵件


郵件常常是Linux下監控報警手段之一。Linux下的mail命令可以方便,快速的完成發送郵件。下面以CentOS為例

1、安裝

復制代碼
[root@vm8028 ~]# mail
-bash: mail: command not found
[root@vm8028 ~]# yum install mailx 
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
base                                                                                                                                                                                                                  | 3.7 kB     00:00     
extras                                                                                                                                                                                                                | 3.4 kB     00:00     
extras/primary_db                                                                                                                                                                                                     |  29 kB     00:00     
updates                                                                                                                                                                                                               | 3.4 kB     00:00     
updates/primary_db                                                                                                                                                                                                    | 4.7 MB     00:04     
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mailx.x86_64 0:12.4-8.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                Arch                                                    Version                                                          Repository                                             Size
=============================================================================================================================================================================================================================================
Installing:
 mailx                                                  x86_64                                                  12.4-8.el6_6                                                     base                                                  235 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install       1 Package(s)

Total download size: 235 k
Installed size: 452 k
Is this ok [y/N]: y
Downloading Packages:
mailx-12.4-8.el6_6.x86_64.rpm                                                                                                                                                                                         | 235 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mailx-12.4-8.el6_6.x86_64                                                                                                                                                                                                 1/1 
  Verifying  : mailx-12.4-8.el6_6.x86_64                                                                                                                                                                                                 1/1 

Installed:
  mailx.x86_64 0:12.4-8.el6_6                                                                                                                                                                                                                

Complete!
[root@vm8028 ~]# mail
No mail for root
復制代碼

2、配置
vi /etc/mail.rc   在文件尾加上如下配置  (注:因為163的設置相對簡單些,以163郵箱為例,QQ郵箱等,其他郵箱因為安全等因素,需要設置的比較多,具體的可以搞下,本文不作重點。QQ郵箱也是可以的)

set from=xd502djj@163.com
set smtp=smtp.163.com
set smtp-auth-user=xd502djj@163.com
set smtp-auth-password=郵箱密碼
set smtp-auth=login


3、發送郵件測試

[root@vm8028 ~]# echo "Content" | mail -s "Title" dongjunjie@cmcm.com
[root@vm8028 ~]#  
[root@vm8028 ~]# 

4、其他

      mail命令 可以使用查看具體更多的使用方法

    mail --h 

      舉例:

     a、無郵件正文  

          # mail -s "Title"  280094177@qq.com

      b、有郵件正文

          #   mail -s "Tile"  280094177@qq.com </root/sms_out.txt 

     或者 cat /root/sms_out.txt  | mail -s "Tile"  280094177@qq.com

         或者 echo “”Content“  |   mail -s "Tile"  280094177@qq.com 

    c 、帶附件的郵件

       mail -s "Tile"  280094177@qq.com  -a  /root/sms_out.txt  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM