通过邮件发送账单详情
1、为了方便财务部分查看个账户支出详情,通过计划任务每天发送账单情况
成都创新互联公司专业为企业提供德惠网站建设、德惠做网站、德惠网站设计、德惠网站制作等企业网站建设、网页设计与制作、德惠企业网站模板建站服务,十多年德惠做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
python 代码如下:
# coding:utf-8
from django.db.models import Q
from jumpserver.api import *
from jumpserver.models import Setting
from django.http import HttpResponse,StreamingHttpResponse
from django.shortcuts import render,render_to_response
from jcloud.aws_iam_scan import *
from jcloud.aws_service_san import *
from jcloud.forms import Aws_monitor_userForm
from jcloud.models import *
import json
import django.utils.timezone as timezone
import time
from datetime import date, timedelta
import boto3
import zipfile
import zlib
import os
import csv
import codecs
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from django.template import loader
from pprint import pprint
def sendmail():
def sendaction(html):#定义发送方法
HOST = "smtp.exmail.qq.com"
TO = "email"
FROM = "email"
msg = MIMEText(html_content,"html","utf-8")
msg['Subject'] = u'AWS云主机账单'
msg['From']=FROM
msg['To']=TO
try:
server = smtplib.SMTP()
server.connect(HOST,"587")
server.starttls()
server.login("user","password")
server.sendmail(FROM, TO, msg.as_string())
server.quit()
print "邮件发送成功!"
except Exception, e:
print "失败:"+str(e)
def rescount(accout_tag):#查询账户数量和服务分类
aws_volumes_list = Aws_volumes.objects.filter(account_tag=accout_tag)
aws_snapshots_list = Aws_snapshots.objects.filter(account_tag=accout_tag)
aws_s3_list = Aws_s3.objects.filter(account_tag=accout_tag)
aws_ec2_list = Aws_ec2.objects.filter(account_tag=accout_tag)
aws_rds_list = Aws_rds.objects.filter(account_tag=accout_tag)
aws_detail = Aws_detail_price.objects.filter(account_tag=accout_tag)
ddict = {}
for d in aws_detail:#遍历所有收费项目,分别累加不同服务价格
ddict[d.service_id] = d.aws_price
list_volume = []#卷使用价格
t_volume = float(0)
for volume in aws_volumes_list:
if volume.VolumeId in ddict.keys():
t_volume = t_volume + float(ddict[volume.VolumeId])
del(ddict[volume.VolumeId])
list_volume.append('volume')
list_volume.append(len(aws_volumes_list))
list_volume.append(t_volume)
# print list_volume
list_snapshots = []#快照价格
t_snapshots = float(0)
for key in ddict.keys():
for snapshots in aws_snapshots_list:
if re.search(snapshots.SnapshotId,key):
t_snapshots = t_snapshots + float(ddict[key])
del(ddict[key])
list_snapshots.append('snapshots')
list_snapshots.append(len(aws_snapshots_list))
list_snapshots.append(t_snapshots)
# print list_snapshots
list_s3 = []#s3价格
t_s3 = float(0)
for s3 in aws_s3_list:
if s3.Name in ddict.keys():
t_s3 = t_s3 + float(ddict[s3.Name])
del(ddict[s3.Name])
list_s3.append('s3')
list_s3.append(len(aws_s3_list))
list_s3.append(t_s3)
# print list_s3
list_ec2 = []#ec2价格
t_ec2 = float(0)
for ec2 in aws_ec2_list:
if ec2.InstanceId in ddict.keys():
t_ec2 = t_ec2 + float(ddict[ec2.InstanceId])
del(ddict[ec2.InstanceId])
list_ec2.append('ec2')
list_ec2.append(len(aws_ec2_list))
list_ec2.append(t_ec2)
# print list_ec2
list_rds = []#rds 价格
t_rds = float(0)
for rds in aws_rds_list:
if rds.DBInstanceArn in ddict.keys():
t_rds = t_rds + float(ddict[rds.DBInstanceArn])
del(ddict[rds.DBInstanceArn])
list_rds.append('rds')
list_rds.append(len(aws_rds_list))
list_rds.append(t_rds)
# print list_rds
list_other = []#其他价格
o_price = float(0)
for o in ddict.keys():
o_price = o_price + float(ddict[o])
list_other.append('Others')
list_other.append(len(ddict))
list_other.append(o_price)
# print list_other
tmp_list = []
tmp2_list = []
sprice = float(0)
for l in [list_ec2,list_volume,list_s3,list_rds,list_snapshots,list_other]:
sprice = sprice + l[2]
tmp_list.append(l)
tmp2_list.append(tmp_list)
tmp2_list.append(dict_tag[accout_tag])
tmp2_list.append(sprice)
if accout_tag == 1:
tmp2_list.append("RMB")
else:
tmp2_list.append("Dollar")
list_res.append(tmp2_list)
dict_tag = {}#区分不同账户资源
dict_tag[1] = '中国:ptmind'
dict_tag[2] = '海外:administrator'
list_res = []
for tag in dict_tag.keys():
rescount(tag)
ttime = time.strftime('%m',time.localtime(time.time()))
html_content = loader.render_to_string('jcloud/sendmail.html',locals())
# print html_content
# return my_render('jcloud/sendmail.html', locals(), request)
# print html_content
sendaction(html_content)
发送页面sendmail.html内容如下:
{{ ttime }}月 AWS 账单
#遍历后台渲染内容
{% for t in list_res %}
{{ t.1 }}
{% for i in t.0 %}
{{ i.0 }}
{{ i.1 }}
{{ i.2 }}
{% endfor %}
{{ t.3 }}: {{ t.2 }}
{% endfor %}
最后展示效果如下:
网站名称:通过邮件发送账单详情
链接URL:http://azwzsj.com/article/gdipoi.html