方法一:簡單郵件發送
echo 'hello world' | mail -s "Subject" -t [email protected],[email protected] -a From:[email protected]
效果截圖
方法二: 文本格式發送郵件
[python] view plaincopyprint?
# !/bin/sh
from_name="from@yanggang"
from="[email protected]"
to="[email protected]"
email_title="Test Mail"
email_content="/home/barry/top800/test/output_email.html"
email_subject="Top800_Games_Free_USA"
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?
# !/bin/sh
from='[email protected]'
to='[email protected]'
email_date=''
email_content='/home/barry/top800/test/output_email.html'
email_subject='Top800_Game_Free_USA'
function send_email(){
email_date=$(date "+%Y-%m-%d_%H:%M:%S")
echo $email_date
email_subject=$email_subject"__"$email_date
echo $email_subject
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
}
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