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 &

相關文章
相關標籤/搜索