shell發郵件

方法一:簡單郵件發送

echo 'hello world' | mail -s "Subject" -t  [email protected],[email protected]    -a From:[email protected]

效果截圖



方法二: 文本格式發送郵件


[python] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片

  1. # !/bin/sh  

  2.   

  3. from_name="from@yanggang"  

  4. from="[email protected]"  

  5. to="[email protected]"  

  6.   

  7. email_title="Test Mail"  

  8. email_content="/home/barry/top800/test/output_email.html"  

  9. email_subject="Top800_Games_Free_USA"  

  10.   

  11. echo -e "To: \"${email_title}\" <${to}>\nFrom: \"${from_name}\" <${from}>\nSubject: ${email_subject}\n\n`cat ${email_content}`" | /usr/sbin/sendmail -t  

效果截圖:





方法三:html格式發送郵件


[python] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片

  1. # !/bin/sh  

  2.   

  3.   

  4. from='[email protected]'  

  5. to='[email protected]'  

  6.   

  7. email_date=''  

  8. email_content='/home/barry/top800/test/output_email.html'  

  9. email_subject='Top800_Game_Free_USA'  

  10.   

  11.   

  12. function send_email(){  

  13.     email_date=$(date "+%Y-%m-%d_%H:%M:%S")  

  14.     echo $email_date  

  15.   

  16.     email_subject=$email_subject"__"$email_date  

  17.     echo $email_subject  

  18.   

  19.     cat $email_content | formail -I "From: $from" -I "MIME-Version:1.0" -I "Content-type:text/html;charset=gb2312" -I "Subject: $email_subject" | /usr/sbin/sendmail -oi $to  

  20.   

  21. }  

  22.   

  23. send_email  

效果截圖:




×××(linux shell 發送email 郵件)


--------------------------------


CentOS是一個非常不錯的免費開源Linux系統,許多站點首選的平臺。

然而CentOS默認不能發送郵件,需要發送郵件的童鞋可以安裝一個sendmail程序。
 
安裝sendmail
執行: [root@vps478753 ~]# yum -y install sendmail
程序會自動搜索出sendmail安裝程序自動安裝,安裝好sendmail以後執行以下命令啓動sendmail
[root@vps478753 ~]# /etc/init.d/sendmail start
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]
 
啓動以後我們可以執行mail命令測試一下是否能發送郵件
一、通過文件內容發送郵件
[root@vps478753 ~]# mail -s 'Test mail' [email protected] < /etc/passwd
很快收到郵件了,正文是 /etc/passwd 文件的內容

 
二、使用管道符直接發送郵件內容
如果不想通過文件發送郵件內容也可以這麼發送
[root@vps478753 ~]# echo "This is test mail" | mail -s 'Test mail' [email protected]
以上效果同文件發送郵件內容一樣

接不到郵件修改hostname

hostname  abc.com
 
如果提示mail: command not found
[root@vps478753 ~]# mail -s 'Test mail' [email protected] < /etc/passwd
-bash: mail: command not found
那麼就是沒有安裝mail命令,此時需要安裝mail命令
[root@vps478753 ~]# yum install mailx -y
然後再重新發送以下郵件就好了!

檢查郵件發送

sendmail -bp


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章