本文介紹如何生成和發(fā)送包含圖表和表格的郵件,涉及echarts圖表轉(zhuǎn)換為圖片、圖片內(nèi)嵌到html郵件內(nèi)容中、html郵件內(nèi)容生成、郵件發(fā)送方法等
一、圖表處理
因?yàn)閔tml格式的郵件不支持echarts,也不支持js執(zhí)行,所以圖表需要轉(zhuǎn)換為圖片內(nèi)嵌在郵件內(nèi)容中
因?yàn)槠脚_(tái)首頁相關(guān)統(tǒng)計(jì)都是使用echarts渲染展示,為了減少后端接口兼容的工作量,我們使用pyecharts替代,這樣就可以使用同樣的數(shù)據(jù)結(jié)構(gòu)和后端接口
pyecharts是python版本的echarts,配置項(xiàng)與echarts基本一致,具體使用方法可以參考官網(wǎng) https://pyecharts.org/#/zh-cn/chart_api
我們實(shí)現(xiàn)的具體代碼如下:
返回是圖片的本地絕對(duì)路徑
需要注意的是,pyecharts render之后生成的是html文件,是通過make_snapshot函數(shù)將html轉(zhuǎn)換為圖片
1、需要安裝庫:pyecharts + snapshot_selenium或者snapshot_phantomjs
2、make_snapshot第一個(gè)參數(shù)是轉(zhuǎn)換引擎,可以使用snapshot_selenium 或者 snapshot_phantomjs
(1)如果使用snapshot_phantomjs,則需要使用npm安裝phantomjs(linux環(huán)境同理)
(2)如果使用snapshot_selenium,則需要將瀏覽器驅(qū)動(dòng)放到python路徑下,比如我使用google瀏覽器,則下載對(duì)應(yīng)版本的驅(qū)動(dòng)chromedriver.exe,放在python.exe所在目錄下(linux環(huán)境同理)
二、郵件內(nèi)容生成
郵件內(nèi)容最終是html格式,需要將圖片和表格等數(shù)據(jù)全部填充好之后再發(fā)送
1、創(chuàng)建模板,需要根據(jù)數(shù)據(jù)實(shí)時(shí)填充的使用{{xxx}}標(biāo)識(shí)
比如我的周報(bào)模板:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>專項(xiàng)檢測(cè)周報(bào)</title>
</head>
<body>
<h2 style="font_size:30">【{{busi_plat}}】風(fēng)險(xiǎn)總數(shù)為:{{risk_total}}</h2>
<hr style="border: none; border-top: 1px solid #ccc; height: 1px; margin: 20px 0;">
<div>
<table style="cellpadding:0;border: 0px solid #ccc;width:100%">
<tr>
<td align="center" valign="top">
<a href="{{risk_level_pic_href}}">
<img src="cid:{{risk_level_pic}}" width="720" height="400" alt="Image 1">
</a>
</td>
<td align="center" valign="top">
<a href="{{risk_status_pic_href}}">
<img src="cid:{{risk_status_pic}}" width="720" height="400" alt="Image 2">
</a>
</td>
</tr>
<tr>
<td align="center" valign="top">
<a href="{{risk_busi_asc_pic_href}}">
<img src="cid:{{risk_busi_asc_pic}}" width="720" height="400" alt="Image 1">
</a>
</td>
<td align="center" valign="top">
<a href="{{risk_check_tool_pic_href}}">
<img src="cid:{{risk_check_tool_pic}}" width="720" height="400" alt="Image 2">
</a>
</td>
</tr>
</table>
</div>
<div>
<hr style="border: none; border-top: 1px solid #ccc; height: 1px; margin: 10px 0;">
<h3 style="font_size:20">【風(fēng)險(xiǎn)類型匯總 TOP 10】</h3>
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<th>序號(hào)</th>
<th>風(fēng)險(xiǎn)類型</th>
<th>發(fā)現(xiàn)工具</th>
<th>風(fēng)險(xiǎn)數(shù)量</th>
</tr>
{{risk_top_data}}
</table>
</div>
<div>
<hr style="border: none; border-top: 1px solid #ccc; height: 1px; margin: 10px 0;">
<h3 style="font_size:20">【軍規(guī)匯總 TOP 10】</h3>
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<th>序號(hào)</th>
<th>軍規(guī)</th>
<th>發(fā)現(xiàn)工具</th>
<th>風(fēng)險(xiǎn)數(shù)量</th>
</tr>
{{army_top_data}}
</table>
</div>
<hr style="border: none; border-top: 1px solid #ccc; height: 1px; margin: 10px 0;">
<h3 style="font_size:20">【本周新增明細(xì)】</h3>
<a href="{{detail_href}}">按照風(fēng)險(xiǎn)等級(jí)排序,最多展示15條,更多詳情和記錄 點(diǎn)這里>>>></a>
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<th>序號(hào)</th>
<th>業(yè)務(wù)方向</th>
<th>業(yè)務(wù)模塊</th>
<th>發(fā)現(xiàn)工具</th>
<th>風(fēng)險(xiǎn)類型</th>
<th>風(fēng)險(xiǎn)明細(xì)</th>
<th>風(fēng)險(xiǎn)等級(jí)</th>
<th>狀態(tài)</th>
<th>操作</th>
</tr>
{{detail_list_data}}
</table>
</body>
</html>
2、獲取到統(tǒng)計(jì)數(shù)據(jù)之后,使用replace做替換,生成最終的html格式的郵件內(nèi)容文章來源:http://www.zghlxwxcb.cn/news/detail-513686.html
三、發(fā)送郵件
在郵件內(nèi)容中使用的圖片需要作為附件一起發(fā)送,才能夠在郵件中正常顯示。
設(shè)置方法參考下圖文章來源地址http://www.zghlxwxcb.cn/news/detail-513686.html
到了這里,關(guān)于發(fā)送圖文并茂的html格式的郵件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!