【python 邮件报表】无附件的邮件报表

简化版:

#coding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from mako.template import Template
from mako.lookup import TemplateLookup
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header

import pandas as pd
import numpy as np
####################设置发送人#######################
sender = '1973536419@qq.com'

######################设置接收人#######################
receiver1 = 'defa.lai@cgtz.com'
receiver2 = '1973536419@qq.com'
receiver3='fang.chen@lg-finance.com'

#####################设置主题#######################
subject = '工资数据报表'


###########################定义渲染模板########################
def mako_render(data, mako_file, directories):
    mylookup = TemplateLookup(directories=directories, input_encoding='utf-8',
                              output_encoding='utf-8',
                              default_filters=['decode.utf_8'])
    mytemplate = Template('<%include file="{}"/>'.format(mako_file),
                          lookup=mylookup, input_encoding='utf-8',
                          default_filters=['decode.utf_8'],
                          output_encoding='utf-8')
    content = mytemplate.render(**data)
    return content

#################设置发送内容:1:发送html表格数据########################

msg = MIMEText("""
<!DOCTYPE html>
<html>
<body>
<div style="text-align:center;width:78.78%;padding: 8px; line-height: 1.42857; vertical-align: top; border-top-width: 1px; border-top-color: rgb(221, 221, 221); background-color: #28a745;color:#fff"><strong>用户工资数据报表</strong></div>

	<table border="1" style="width:80%;">
  <tr style="background-color:pink;text-align:center;">
    <th>月份</th>
    <th>存款</th>
    <th>工资</th>
    <th>年薪</th>


  </tr>
  <tr  style="text-align:center;">
    <td>一月</td>
    <td>1000 元</td>
    <td>3000 元</td>
    <td>12000元</td>

  </tr>
  <tr  style="text-align:center;">
    <td>二月</td>
    <td>1500 元</td>
    <td>4000 元</td>
    <td>16000 元</td>
  </tr>
</table>

</body>
</html>





""",'html','utf-8')

# #########################设置发送内容2:发送本地excel文件############################
#
# data = pd.read_excel("c:/pic/wang.xlsx")
# kk = len(data)
# a = np.array(data.iloc[:, 1:7], dtype=str)
# rows_data = a.tolist()
# row_headers = []
# col_headers = ['日期', '排名', '平台名称', '成交量(万元)', '平均利率(%)', '平均借款期限(月)', '累计待还款金额(万元)']
# for i in range(0, kk):
#     row_headers.append('2016-05-29')
# data = {'rows_data': rows_data, 'row': col_headers,
#         'row_headers': row_headers}
#
#
# tmpl_directories = 'c:/pic/tmpl'
# csv_file = "c:/pic/wang.xlsx"
# content = mako_render(data, 'statistics.txt', directories=tmpl_directories)
# msg=MIMEText(content,'html','utf-8')




############################设置一些附属表头参数#############################
msg['From']=formataddr(["菜鸟教程",sender])
msg['To']=formataddr(["赖德发",receiver1])
msg['To']=formataddr(["开心果汁",receiver2])
msg['To']=formataddr(["陈芳",receiver3])


msg['Subject'] = Header(subject, 'utf-8')

###################################登陆邮箱发送##################################
username = '1973536419@qq.com'
password = 'XXXXXXXXXXXXX'


#################默认传输#################
smtp = smtplib.SMTP_SSL("smtp.qq.com")


#############################加密传送###################
# smtp_server = 'smtp.qq.com'
# smtp_port = 587
# smtp = smtplib.SMTP(smtp_server, smtp_port)
# smtp.starttls()
############################################


smtp.login(username, password)
smtp.sendmail(sender, [receiver1,receiver2,receiver3], msg.as_string())
smtp.quit()

简单邮件发送报警模块:

# -*- encoding=utf-8 -*-


import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header


def email_alarm():
    # 发送人
    sender = '1973536419@qq.com'
    # 接收人
    receiver1 = '1973536419@qq.com'
    # 设置主题
    subject = '钉钉报警服务'
    # 设置发送内容
    msg = MIMEText('钉钉报警和电话报警服务挂了@东篱')

    #设置一些附属表头参数
    msg['From']=formataddr(["大数据报警",sender])
    msg['To']=formataddr(["东篱",receiver1])
    msg['Subject'] = Header(subject, 'utf-8')

    #登陆qq邮箱发送
    username = '1973536419@qq.com'
    password = '此处填写你的授权码'
    smtp = smtplib.SMTP_SSL("smtp.qq.com",465)
    smtp.login(username, password)
    smtp.sendmail(sender,receiver1, msg.as_string())
    smtp.quit()


if __name__ == '__main__':
    email_alarm()


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页