#!/usr/bin/python3
import smtplib
import urllib.request
import time
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

your_ip = "192.168.0.177"
your_ip_optional_port = ":8080"
port_to_camera = ":8081"
sending_email_username = "youre_mail_username"
sending_email_password = "your_password"
to = "receiving_email@gmail.com"

try:
 print("create email")
 msg = MIMEMultipart()
 msg['Subject'] = "OMFG Intruder!"
 msg['From'] = "Laser Security System"
 msg['To'] = "OneBadApple"
 msg.preamble = "this is the preamble"


 
 html = """\
	<html>
		<head></head>
		<body>
			<p>	Warning! Laser trip wire was triggered.
				<br>The snapshot below was taken at the time of the event.
				<br>To view a live stream of the camera click on the picture or the link.
		
				<br>
				<br>
				<a href="http://""" + your_ip + your_ip_optional_port + """/index.html">
					<img height="300" src="cid:image1">
				</a>
    <br>
				<br>
				<a href="http://""" + your_ip + your_ip_optional_port + """/index.html">
					Show me the intruder!
				</a>
			</p>
		</body>
	</html>
	"""

 try:
  print("take snapshot")

  # If you have trouble with a hang on the line below make sure your /etc/network/interfaces file contains
  # the following lines to allow loopback. I spent like 3 hrs trying to solve this problem
  #
  # auto lo
  # iface lo inet loopback
 
  file = urllib.request.urlopen("http://" + your_ip + port_to_camera + "/?action=snapshot").read()	 
  print ("format image")
  msgImage = MIMEImage(file)
  print ("done format image")
  
  
 except Exception:
  print ("couldnt read image")
  os.system("ifdown eth0")
  time.sleep(2)
  os.system("ifup eth0")
  exit()


 print ("build the email")
 msgImage.add_header('Content-ID','<image1>')
 msg.attach(msgImage)

 html_part = MIMEText(html,"html")
 msg.attach(html_part)

 print ("send the email")
 mserv = smtplib.SMTP() 
 mserv.set_debuglevel(1) # comment this line out if you want to clean up the terminal output. it is handy to debug your email connection though

 # see this page for setting up the next line https://support.google.com/a/answer/176600?hl=en
 mserv.connect('smtp.gmail.com',587)

 print ("successful connection")

 mserv.ehlo()
 mserv.starttls()

 print ("log into sending email")
 #as to not compromise my own email address I created a dummy gmail account as a test bed. 
 # if you run into problems you will have to change your email settings to allow less secure applications to use it
 # see this discussion http://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python
 mserv.login(sending_email_username,sending_email_password)
 print("successful login")
 
 mserv.sendmail(sending_email_username,to,msg.as_string())
 mserv.close()
 print ("sent email")
 os._exit(0)
except Exception as e:
 print (e)
 print ("Coult not send mail for laser!")
 print ("Check network connnection.")
 os._exit(0)