Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # Send an HTML email with an embedded image and a plain text message for
  2. # email clients that don't want to display the HTML.
  3.  
  4. import datetime
  5. import time
  6. import smtplib
  7. import urllib2
  8. import RPi.GPIO as gpio
  9. gpio.setmode(gpio.BCM)
  10. gpio.setup(17, gpio.IN, pull_up_down = gpio.PUD_DOWN)
  11.  
  12.  
  13. from email.MIMEMultipart import MIMEMultipart
  14. from email.MIMEText import MIMEText
  15. from email.MIMEImage import MIMEImage
  16.  
  17. dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
  18. print 'SMTP Doorbell Application loaded @ ' + dtnow
  19.  
  20. smtpServer = 'smtp.server.com'
  21. smtpPort = '587'
  22.  
  23. smtpUser = 'smtuser@email.com'
  24. smtpPass = 'P@ssword1'
  25.  
  26. strFrom = 'fromname@email.com'
  27. strTo = 'toname@email.com'
  28.  
  29.  
  30. # Create the root message and fill in the from, to, and subject headers
  31. msgRoot = MIMEMultipart('related')
  32. msgRoot['Subject'] = 'RPI - Doorbell Alert'
  33. msgRoot['From'] = strFrom
  34. msgRoot['To'] = strTo
  35. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  36.  
  37. header = 'From: ' + strFrom + 'n' + 'To: ' + strTo + 'n' + 'Subject: RPI - Doorbell Alert'
  38.  
  39.  
  40. while True:
  41. input_value = gpio.input(17)
  42. if input_value == False:
  43.  
  44. print("33c")
  45. dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
  46. print 'Doorbell has been pressed @ ' + dtnow
  47.  
  48. # Encapsulate the plain and HTML versions of the message body in an
  49. # 'alternative' part, so message agents can decide which they want to display.
  50. msgAlternative = MIMEMultipart('alternative')
  51. msgRoot.attach(msgAlternative)
  52.  
  53. dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
  54. msgText = MIMEText('Doorbell Rang @ ' + dtnow)
  55. msgAlternative.attach(msgText)
  56.  
  57. # We reference the image in the IMG SRC attribute by the ID we give it below
  58. msgText = MIMEText('Doorbell Rang @ ' + dtnow + ' <br><img src="cid:image1">', 'html')
  59. msgAlternative.attach(msgText)
  60.  
  61.  
  62. # Get ReoLink Image
  63. request = urllib2.Request(
  64. r'http://192.168.0.11/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=12345',
  65. headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
  66. page = urllib2.urlopen(request)
  67. with open('doorbell.png','wb') as f:
  68. f.write(page.read())
  69.  
  70. # This example assumes the image is in the current directory
  71. fp = open('doorbell.png', 'rb')
  72. msgImage = MIMEImage(fp.read())
  73. fp.close()
  74.  
  75. # Define the image's ID as referenced above
  76. msgImage.add_header('Content-ID', '<image1>')
  77. msgRoot.attach(msgImage)
  78.  
  79. # Send the email (this example assumes SMTP authentication is required)
  80. smtp = smtplib.SMTP(smtpServer, smtpPort)
  81.  
  82. smtp.ehlo()
  83. smtp.starttls()
  84. smtp.ehlo()
  85.  
  86. smtp.login(smtpUser,smtpPass)
  87. smtp.sendmail(strFrom, strTo.split(','), msgRoot.as_string())
  88. time.sleep(15)
  89. smtp.quit()
  90.  
  91. print 'SMS sent successful!'
  92. print header + 'n' + 'Message: Doorbell Rang @ ' + dtnow
  93.  
  94. while input_value == False:
  95. input_value = gpio.input(17)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement