
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import os
import random
import string
from envelopes import Envelope, SMTP, GMailSMTP
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
def get_url(img):
# 需要填写你的 Access Key 和 Secret Key
access_key = '你的 Access Key'
secret_key = '你的 Secret Key'
# 要上传的空间
bucket_name = '要上传的空间'
bucket_domain = 'http://空间/'
# 构建鉴权对象
q = Auth(access_key, secret_key)
# 上传到七牛后保存的文件名
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 32))
key = ran_str+'_netstat.png'
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
# img:要上传文件的本地路径
ret, info = put_file(token, key, img)
# print(info)
assert ret['key'] == key
assert ret['hash'] == etag(img)
return bucket_domain+key
def mail_out(mail_title, mail_body, mail_attachment1, mail_attachment2, mail_attachment3):
# 构造envelop
envelope = Envelope(
from_addr=('from@com.com', 'dev'),
to_addr=('tttt', 'ttt'),
subject=mail_title,
# text_body ='envelope is a python mail module'
html_body=mail_body
)
# 添加附件
envelope.add_attachment(mail_attachment1)
envelope.add_attachment(mail_attachment2)
envelope.add_attachment(mail_attachment3)
# 发送邮件
# 发送邮件方法1
envelope.send('smtp.tttt', '25', login='ttttt', password='tttt')
# 发送邮件方法2
# mail = SMTP(host='ttt', port='25',login ='tttt', password ='tttt')
# mail.send(envelope)
def mail_create():
# mail
os.system('vnstati -i eth0 --months --output /root/month.png')
os.system('vnstati -i eth0 --days --output /root/days.png')
os.system('vnstati -i eth0 --hours --output /root/hours.png')
os.system('> /root/mailcontent ')
#os.system('echo -e "The report of `date +%Y-%m-%d`\t" >> /root/mailcontent ')
os.system('uptime >>/root/mailcontent ')
os.system('echo -e "\t">>/root/mailcontent ')
#os.system('vnstat >>/root/mailcontent ')
os.system('echo -e "\t">>/root/mailcontent ')
with open('/root/mailcontent') as file:
mail_content=file.read()
img_hours=get_url('/root/hours.png')
img_days=get_url('/root/days.png')
img_month=get_url('/root/month.png')
mail_title = '10.0.1.1 网络报告'
mail_boady=mail_content+'<div><img src='+img_hours+'></div>'+'<div><img src='+img_days+'></div>'+'<div><img src='+img_month+'></div>'
mail_attachment1 = '/root/hours.png'
mail_attachment2 = '/root/days.png'
mail_attachment3 = '/root/month.png'
mail_out(mail_title, mail_boady, mail_attachment1, mail_attachment2, mail_attachment3)
if __name__ == '__main__':
# 指定编码格式
reload(sys)
sys.setdefaultencoding('utf8')
# main
mail_create()