Skip to content

Instantly share code, notes, and snippets.

@jizhang
Last active December 18, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jizhang/5749612 to your computer and use it in GitHub Desktop.
Save jizhang/5749612 to your computer and use it in GitHub Desktop.
1. Login to Zabbix; 2. Fetch image; 3. Send email.
# -*- 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'] = 'shzhangji@163.com'
msg['To'] = 'jizhang@anjuke.com'
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('shzhangji@163.com', 'password')
s.sendmail('shzhangji@163.com', 'jizhang@anjuke.com', msg.as_string())
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment