Created
May 10, 2016 09:57
-
-
Save boncheff/bfedfcc3fbb324916718b2cbfb0926d2 to your computer and use it in GitHub Desktop.
Python script that uses PhantomJS to create screenshots of HTML elements and sends an email using gmail
This file contains hidden or 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
import smtplib | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
from email.MIMEImage import MIMEImage | |
import os | |
widgets = [ | |
{ | |
'name': 'Cloud Summary', | |
'output_file': 'cloud_summary.png', | |
}, | |
{ | |
'name': 'Performance Graph', | |
'output_file': 'performance_graph.png', | |
}, | |
{ | |
'name': 'Server Power State', | |
'output_file': 'server_power_state.png', | |
}, | |
{ | |
'name': 'Servers by Provider', | |
'output_file': 'servers_by_provider.png', | |
}, | |
] | |
def send_email(): | |
fromaddr = "FROM_ADDR" | |
toaddr = "TO_ADDR" | |
msg = MIMEMultipart() | |
msg['From'] = fromaddr | |
msg['To'] = toaddr | |
msg['Subject'] = "SUBJECT" | |
msg.attach(MIMEText('This is the body')) | |
for widget in widgets: | |
msgText = MIMEText('<img src="cid:%s">' % widget['output_file'], 'html') | |
msg.attach(msgText) | |
filename = widget['output_file'] | |
fp = open(filename, "rb") | |
msgImage = MIMEImage(fp.read()) | |
msgImage.add_header('Content-ID', '<' + widget['output_file'] + '>') | |
msg.attach(msgImage) | |
fp.close() | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.ehlo() | |
server.starttls() | |
server.login(fromaddr, "PASSWORD") | |
text = msg.as_string() | |
server.sendmail(fromaddr, toaddr, text) | |
server.quit() | |
def generate_screenshots(): | |
os.system('phantomjs --ignore-ssl-errors=yes screenshotter.js') | |
def main(): | |
print('generating') | |
generate_screenshots() | |
print('done') | |
print('sending') | |
send_email() | |
print('done') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment