成都网站建设设计

将想法与焦点和您一起共享

通过邮件发送账单详情-创新互联

1、为了方便财务部分查看个账户支出详情,通过计划任务每天发送账单情况

创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供成都网站制作、成都网站设计、外贸营销网站建设、成都网页设计、微信平台小程序开发、成都网站开发、成都网站制作、成都软件开发、app软件定制开发是成都本地专业的网站建设和网站设计公司,等你一起来见证!

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 %}
{% for i in t.0 %} {% endfor %}

{{ t.1 }}


{{ i.0 }} {{ i.1 }} {{ i.2 }}

{{ t.3 }}: {{ t.2 }}

{% endfor %}

最后展示效果如下:
通过邮件发送账单详情

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:通过邮件发送账单详情-创新互联
分享地址:http://chengdu.cdxwcx.cn/article/djccpj.html