In [12]:
# The public plotly graphs to include in the email. These can also be generated with `py.plot(figure, filename)`
graphs = [
    #First graph is a breakdown of all 311 requests into request type
    'https://plot.ly/~phillipwong/2',
    
    #Second graph is how the requests were concentrated for each week of the year 2016
    'https://plot.ly/~phillipwong/33',
    
    #Third graph is a breakdonw of request type for each specific week over the three years
    'https://plot.ly/~phillipwong/37',
    
    #2015 Weekly comparison
    'https://plot.ly/~phillipwong/49',
    
    #2016 Weekly comparison
    'https://plot.ly/~phillipwong/41',
    
    #2017 weekly comparison
    'https://plot.ly/~phillipwong/43',
    
    #2018 Weekly comparison
    'https://plot.ly/~phillipwong/45',
    
    #2015-2018 Weekly Comparison
    'https://plot.ly/~phillipwong/53'
]

In [13]:
from IPython.display import display, HTML

template = (''
    '<a href="{graph_url}" target="_blank">' # Open the interactive graph when you click on the image
        '<img src="{graph_url}.png">'        # Use the ".png" magic url so that the latest, most-up-to-date image is included
    '</a>'
    '{caption}'                              # Optional caption to include below the graph
    '<br>'                                   # Line break
    '<a href="{graph_url}" style="color: rgb(190,190,190); text-decoration: none; font-weight: 200;" target="_blank">'
        'Click to comment and see the interactive graph'  # Direct readers to Plotly for commenting, interactive graph
    '</a>'
    '<br>'
    '<hr>'                                   # horizontal line
'')

email_body = 'This is a test of the 311 Email Service. This will be filled later with more details and information.'
for graph in graphs:
    _ = template
    _ = _.format(graph_url=graph, caption='')
    email_body += _

display(HTML(email_body))





In [14]:
me  = 'phillip.wong16@gmail.com'
recipient = 'pwong@oxy.edu'
subject = 'LA 311 Weekly Email Report'

email_server_host = 'smtp.gmail.com'
port = 587
email_username = me
email_password = '10375Melissa'

In [15]:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os

msg = MIMEMultipart('alternative')
msg['From'] = me
msg['To'] = recipient
msg['Subject'] = subject


msg.attach(MIMEText(email_body, 'html'))

server = smtplib.SMTP(email_server_host, port)
server.ehlo()
server.starttls()
server.login(email_username, email_password)
server.sendmail(me, recipient, msg.as_string())
server.close()