centos发送邮件

版权声明:本文为博主原创文章,未经博主允许不得转载。有问题可加微信meizu_mx4 https://blog.csdn.net/sinat_15955423/article/details/81662652

  最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程。

一、安装sendmail与mail

  1、安装sendmail:

     1) centos下可以安装命令:yum -y install sendmail

     2) 安装完后启动sendmail命令:service sendmail start

  2、安装mail

     安装命令:yum install -y mailx

二、发送邮件

    1、通过文件内容发送

     发送命令:mail -s 'mail test' [email protected] < con.txt ("mail test"为邮件主题,[email protected]为收件人邮箱,con.txt保存邮件内容)

  2、通过管道符直接发送

     发送命令:echo "this is my test mail" | mail -s 'mail test' [email protected]

三、设置发件人信息

  上述发送邮件默认会使用linux当前登录用户信,通常会被当成垃圾邮件,指定发件人邮箱信息命令:vi /etc/mail.rc,编辑内容如:

set [email protected]
set smtp=smtp.126.com
set smtp-auth-user=username
set smtp-auth-password=password
set smtp-auth=login

   注意配置中的smtp-auth-password不是邮箱登录密码,是邮箱服务器开启smtp的授权码,每个邮箱开启授权码操作不同(网易126邮箱开启菜单:设置-> 客户端授权密码)。

我的脚本内容是:

#!/bin/sh
cd /home/joyulf/mysql_data_back
echo "已切换到备份目录,开始备份..."
mv bakmysql* /home/joyulf/mysql_data_back
echo "旧文件已保存到/home/joyulf/mysql_data_back"
Now=$(date +"%Y-%m-%d")
Mysqlpd="root"
File=bakmysql-$Now.sql.gz
echo "开始dump数据库..."
mysqldump -uroot -p$Mysqlpd dms_erp | gzip > $File
echo "dump success!开始远程复制..."
passwd="root"
echo "复制全部完成!即将退出..."
if [ $? -ne 0 ]; then
    echo "备份失败"
else
    echo "备份成功"|mail -s $Now-数据库备份 -a $File [email protected]
fi
 

猜你喜欢

转载自blog.csdn.net/sinat_15955423/article/details/81662652