Centos通过mail服务发送邮件

                                               Centos通过mail服务发送邮件

1.centos安装mail服务
yum -y install mailx

2.创建ssl文件
(1) mkdir -p /root/.mailssl/
 #####创建目录,用来存放证书
(2) echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.mailssl/163.crt
#####向163请求证书
(3) certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.mailssl -i ~/.mailssl/163.crt
#####添加一个证书到证书数据库中
(4) certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.mailssl/163.crt
#####添加一个证书到证书数据库中!
(5) certutil -L -d /root/.certs
#####列出目录下证书
Centos通过mail服务发送邮件

3.修改/etc/mail.cy ;添加以下内容:

      set from=***********@163.com  
      set smtp=smtps://smtp.163.com:465  
      set smtp-auth-user=********@163.com
      set smtp-auth-password=********* 
      set smtp-auth=login 
      set nss-config-dir=/root/.certs  
      set ssl-verify=ignore 

---说明
from:对方收到邮件时显示的发件人
smtp:指定第三方发邮件的smtp服务器地址
set smtp-auth-user:第三方发邮件的用户名
set smtp-auth-password:用户名对应的密码,有些邮箱填的是授权码
smtp-auth:SMTP的认证方式,默认是login,也可以改成CRAM-MD5或PLAIN方式
nss-config-dir: SSL验证信息
ssl-verify: SSL验证信息
4.现在发送测试邮件
echo "正文" | mail -s "标题" 收件人邮箱

  1. 看起来已经成功了,但是发送完邮件还有报错:证书不被信任,且命令行就此卡住,需要按键才能出现命令提示符
     Error in certificate: Peer's certificate issuer is not recognized.
    按照下面走:
    (1)cd /root/.mailssl/
    (2)certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt
    Centos通过mail服务发送邮件

6.再次发送邮件
echo "hallo" | mail -v -s "this is Test Mail" [email protected]
7.送大家一个shell脚本,在crontab建立一个定时任务每10分钟去执行此脚本
脚本内容比较简单,希望你们能用的上(仅代表个人观点)

#!/bin/bash
url="200"
curl=`curl -I  http://网站url| grep 200 | awk '{print $2}'`
if [ "$url" == "$curl" ];then
   echo "网站安然无恙"
else
    if [ "$url" != "$curl" ];then
   `echo "正文" | mail -s "网站严重错误" [email protected]`
    fi
fi

猜你喜欢

转载自blog.51cto.com/12832314/2125950