Linux mail邮件附件定时发送

一:原理mysql

一、shell实现mysql数据导出sql

二、导出的数据文件转码(utf8-tgb2312)shell

三、邮件定时发送vim

二:环境搭建postfixcentos

rpm -qa |grep postfix  确认postfix是否安装bash

安装postfix服务器

yum -y install postfix dom

更改默认MTA为Postfix:
/usr/sbin/alternatives --set mta /usr/sbin/sendmail.postfix
alternatives --display mtapost

vim /etc/postfix/main.cf     直接在尾部添加便可编码

myhostname = ****   ##主机名字
mydomain = ***.com        ##mstp邮箱服务器域名,下面的随意,存在就行
#mydomain = qq.com
myorigin = $mydomain      
inet_interfaces = all
mydestination = ***@$mydomain,***@$mydomain,***@$mydomain
#mydestination = test@$mydomain
mynetworks = 192.168.100.10/24,127.0.0.0/8,172.16.0.0/24
relay_domains =
home_mailbox = Maildir/

启动服务

systemctl  start postfix.service    (centos7.1)

三:shell获取数据及发送邮件

#!/bin/bash

current_dir="/data/scripts/cipm_import"
today=`date +"%Y%m%d"`
host_IP="*******"
host_user="*******"
host_password="*********"
mysql_comm="/bin/mysql"
msyql_db="*****"
mail_theme="业务数据按期导出"
mail_content="***对应***数据
***对应***数据
***对应协议数据
***对应支付通道数据
***对应供应商数据"
mail_user1="******"
mail_user2="*******"
mail_user3="********"

##数据导出以及文件转码成excel能识别的编码
report_export () {
     cd $current_dir

     while read line 
     do
          report_name=`echo "$line"|cut -d" " -f2`
          case $report_name in
          a.ediname)
               report_name=edi
               ;;
          a.elfname)
               report_name=robot
               ;;
          a.agreementcode)
               report_name=protocol
               ;;
          b.paychannelname)
               report_name=pay
               ;;
          *)
               report_name=cipm
               ;;
          esac

          $mysql_comm -h $host_IP -u$host_user -p$host_password -e "use $msyql_db;$line"  > "$report_name""$today".xls      ###sql从文件按行输入
          iconv -futf8 -tgb2312 -o "$report_name""$today"_back.xls "$report_name""$today".xls
     done < $current_dir/sqlyuju      ###导出的数据文件转码
 }

mail_send () {
     cd $current_dir
     tar -zcf data"$today".tar.gz *back*
     mail_fujian=data"$today".tar.gz
     echo “$mail_content” |mail -s "$mail_theme" -a $mail_fujian -c $mail_user1 $mail_user2 $mail_user3    ###邮件发送  -a  发送的附件   -c 第一个为抄送人   后面接的都是邮件接收人
     rm -f *.xls $mail_fujian       
}

report_export
mail_send

四:定时执行

crontab -e

05 12 * * 1 /bin/bash   /data/scripts/cipm_import/data_import.sh  > /dev/null 2>&1 &

相关文章
相关标签/搜索