Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
python_sendmail/sendEmail.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
86 lines (69 sloc)
2.79 KB
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
from datetime import date | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email.header import Header | |
from email import encoders | |
import requests | |
from PIL import Image | |
import shutil | |
import smtplib | |
url = 'http://librenms.tcom.purdue.edu/api/v0/devices/2073/device_cras_sessions' | |
headers = { 'X-Auth-Token' : 'TOKEN_HERE' } | |
res = requests.get(url, headers=headers, stream=True) | |
if res.status_code == 200: | |
with open('./image1.png', 'wb') as f: | |
for chunk in res: | |
f.write(chunk) | |
im = Image.open('./image1.png') | |
fill_color = (255,255,255) | |
im = im.convert('RGBA') | |
if im.mode in ('RGBA', 'LA'): | |
background = Image.new(im.mode[:-1], im.size, fill_color) | |
background.paste(im, im.split()[-1]) # omit transparency | |
im = background | |
im.convert('RGB').save('./image1White.png') | |
url = 'http://librenms.tcom.purdue.edu/api/v0/devices/3517/device_cras_sessions' | |
headers = { 'X-Auth-Token' : 'TOKEN_HERE' } | |
res = requests.get(url, headers=headers, stream=True) | |
if res.status_code == 200: | |
with open('./image2.png', 'wb') as f: | |
for chunk in res: | |
f.write(chunk) | |
im = Image.open('./image2.png') | |
fill_color = (255,255,255) | |
im = im.convert('RGBA') | |
if im.mode in ('RGBA', 'LA'): | |
background = Image.new(im.mode[:-1], im.size, fill_color) | |
background.paste(im, im.split()[-1]) # omit transparency | |
im = background | |
im.convert('RGB').save('./image2White.png') | |
cd = date.today() | |
cd = cd.strftime("%m/%d/%Y") | |
sender = 'itap-dnet-tier2@lists.purdue.edu' | |
recipients = ['pnoc-alerts@purdue.edu', 'example@test.com'] | |
msg = MIMEMultipart() | |
msg['From'] = 'itap-dnet-tier2@lists.purdue.edu' | |
msg['To'] = ", ".join(recipients) | |
msg['Subject'] = Header('WebVPN Connection Graphs ' + cd, 'utf-8').encode() | |
with open('./image1White.png', 'rb') as f: | |
mime = MIMEBase('image1', 'png', filename='image1White.png') | |
mime.add_header('Content-Disposition', 'attachment', filename='image1White.png') | |
mime.add_header('X-Attachment-Id', '0') | |
mime.add_header('Content-ID', '<0>') | |
mime.set_payload(f.read()) | |
encoders.encode_base64(mime) | |
msg.attach(mime) | |
with open('./image2White.png', 'rb') as f: | |
mime = MIMEBase('image2', 'png', filename='image2White.png') | |
mime.add_header('Content-Disposition', 'attachment', filename='image2White.png') | |
mime.add_header('X-Attachment-Id', '1') | |
mime.add_header('Content-ID', '<1>') | |
mime.set_payload(f.read()) | |
encoders.encode_base64(mime) | |
msg.attach(mime) | |
msg.attach(MIMEText('<html><body><h1>WebVPN Sessions:</h1><p><img src="cid:0"></p><h1>WebVPN2 Sessions:</h1><p><img src="cid:1"></p></body></html>', 'html', 'utf-8')) | |
server = smtplib.SMTP('smtp.purdue.edu', 25) | |
server.set_debuglevel(1) | |
server.sendmail(sender, recipients, msg.as_string()) | |
server.quit() |