html发送邮件_在LINUX下面发送HTML格式的邮件

需求:用linux自带的邮件服务发送HTML格式的邮件,以下是最终的结果

dfd21be249457b2d87dee0fe8deef716.png

1) 将要发送的内容从mysql取出来并生成.txt文件

backuplog.sh

#!/bin/bash

#mydumper多线程数据备份

export PATH=/usr/local/bin:/usr/local/mysql/bin:$PATH

#配置表及job信息存放位置

HOSTNAME="localhost"

PORT="3306"

USERNAME="root"

PASSWORD="123456"

today=`date "+%G%m%d"`

for ip in $(mysql -u $USERNAME -p$PASSWORD -h $HOSTNAME -P $PORT -e "select concat(ip,',',port,',',backupstatus) from mysqlcenter.backuplog where backupstatus='SUCCESS' and btype='end backup' and btime> DATE_FORMAT(NOW(),'%Y-%m-%d')union all select concat(ip,',',port,',',backupstatus) from mysqlcenter.backuplog where backupstatus='FAILED' and btype='end backup' and btime> DATE_FORMAT(NOW(),'%Y-%m-%d');

"|sed '1d')

do

echo $ip >> backuplog"$today".txt

done

最后保存并赋权chmod 777 sendmail.sh

2) 对上一步生成的.txt生成html格式的

sendmail.sh

#!/bin/bash

#FILE1_PATH="/root/wsktest/2.png"

FILE1_PATH=""

EMAIL_

today=`date "+%G%m%d"`

html_input(){

echo "

$1$2$3" >>/data/email/mail.html

}

html_input_red(){

echo "

$1$2$3" >>/data/email/mail.html

}

set_info(){

i=1

echo "

table_namesource_counttarget_count

table_counts=$(awk -F "," '{print $1}' /data/email/backuplog"$today".txt) #表计数集合

for tablename in $table_counts

do

j=2

html_sour_count=$(awk -F "," 'NR==i { print $j}' i=$i j=$j /data/email/backuplog"$today".txt) #mysql中对应的计数

let "j++"

html_tar_count=$(awk -F "," 'NR==i { print $j}' i=$i j=$j /data/email/backuplog"$today".txt) #数仓中对应的计数

if [ "$html_sour_count" == "$html_tar_count" ];then

html_input $tablename $html_sour_count $html_tar_count #构造每行表格信息

else

html_input_red $tablename $html_sour_count $html_tar_count #构造每行表格信息

fi

let "i++"

echo $tablename $html_sour_count $html_tar_count $i $j

done

echo "

" >> /data/email/mail.html

}

set_info

EMAIL_EXCEL=$(cat /data/email/mail.html)

最后赋权chmod 777 backuplog.sh

3) 发送邮件

mail -s "$(echo -e "testContent-Type: text/html;charset=gb2312")" "ji@163.com"

4) 把这三步合并在一起成一步

email.sh

#!/bin/bash

bash /data/email/backuplog.sh

bash /data/email/sendmail.sh

cd /data/email

mail -s "$(echo -e "testContent-Type: text/html;charset=gb2312")" "ji@163.com"

最后赋权chmod 777 email.sh

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页