Last active
December 18, 2015 07:49
-
-
Save jizhang/5749612 to your computer and use it in GitHub Desktop.
1. Login to Zabbix;
2. Fetch image;
3. Send email.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
import urllib2 | |
import smtplib | |
from email.mime.image import MIMEImage | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
data = { | |
'jsonrpc': '2.0', | |
'method': 'user.login', | |
'params': { | |
'user': 'admin', | |
'password': 'zabbix' | |
}, | |
'id': 1 | |
} | |
req = urllib2.Request(url='http://zabbix/api_jsonrpc.php', | |
data=json.dumps(data), | |
headers={'Content-Type': 'application/json-rpc'}) | |
f = urllib2.urlopen(req) | |
token = json.loads(f.read())['result'] | |
f.close() | |
req = urllib2.Request(url='http://zabbix/chart2.php?graphid=575', | |
headers={'Cookie': 'zbx_sessionid=%s' % token}) | |
f = urllib2.urlopen(req) | |
img = f.read() | |
f.close() | |
msg = MIMEMultipart('related') | |
msg['Subject'] = 'Test Image' | |
msg['From'] = '[email protected]' | |
msg['To'] = '[email protected]' | |
msg.attach(MIMEText('Hi, <strong>Jerry</strong>!<br><img src="cid:img1">', 'html')) | |
mimg = MIMEImage(img, 'png') | |
mimg.add_header('Content-ID', '<img1>') | |
msg.attach(mimg) | |
s = smtplib.SMTP('smtp.163.com') | |
s.login('[email protected]', 'password') | |
s.sendmail('[email protected]', '[email protected]', msg.as_string()) | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment