实用的知识教程
汇智娱乐万赢棋牌游戏大全 > 网络知识 >  正文

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

2020-01-11 11:00:43 来源: 汇智娱乐万赢棋牌游戏大全 记者 汇智娱乐万赢棋牌游戏大全

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

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_TITLE="测试"

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

html_input(){

echo "<tr>

<td>$1</td>

<td>$2</td>

<td>$3</td>

</tr>" >>/data/email/mail.html

}

html_input_red(){

echo "<tr bgcolor="#FF0000">

<td>$1</td>

<td>$2</td>

<td>$3</td>

</tr>" >>/data/email/mail.html

}

set_info(){

i=1

echo "

<table border=1 border=1 cellspacing='0' cellpadding='0' >

<tr>

<th>table_name</th>

<th>source_count</th>

<th>target_count</th>

</tr>" > /data/email/mail.html

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 "</table>" >> /data/email/mail.html

}

set_info

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

最后赋权chmod 777 backuplog.sh

3) 发送邮件

mail -s "$(echo -e "testnContent-Type: text/html;charset=utf-8")" "ji@163.com" <mail.html

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

email.sh

#!/bin/bash

bash /data/email/backuplog.sh

bash /data/email/sendmail.sh

cd /data/email

mail -s "$(echo -e "testnContent-Type: text/html;charset=utf-8")" "ji@163.com" <mail.html

最后赋权chmod 777 email.sh



温馨提示如有转载或引用以上内容之必要,敬请将作为出处标注,谢谢合作!

相关新闻
凡注有"汇智娱乐万赢棋牌游戏大全"的稿件,均为汇智娱乐万赢棋牌游戏大全独家版权所有,未经许可不得转载或镜像;授权转载必须注明来源为"汇智娱乐万赢棋牌游戏大全",并保留"汇智娱乐万赢棋牌游戏大全"。
博评网