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


你可能感兴趣的:(shell发邮件)