<返回更多

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

2019-12-18    
加入收藏

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

在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=gb2312")" "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=gb2312")" "ji@163.com" <mail.html

最后赋权chmod 777 email.sh

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>