-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12 Started implementing email notifications
EmailSender class was created which sends the image to the given receiver. The class constructor receives the camera number and data from the camera in byte format, saves the image to temp folder and sends an email with the image as attachment. Until a better solution is found, the email is sent from newly created gmail account and through gmail's SMTP server.
- Loading branch information
1 parent
0ec907a
commit 52ca4fb
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import cv2 | ||
import numpy as np | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from email.mime.base import MIMEBase | ||
from email import encoders | ||
from datetime import datetime | ||
|
||
class EmailSender(object): | ||
|
||
def __init__(self, camera_num, img_data): | ||
self.data = img_data | ||
self.camera_num = camera_num | ||
|
||
def send_email(self, receiver_mail): | ||
filename = 'face.jpg' | ||
path = '/tmp/' + filename | ||
sender = "sissurvailance@gmail.com" | ||
receiver = receiver_mail | ||
nparr = np.fromstring(self.data, np.uint8) | ||
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | ||
cv2.imwrite(path, img_np) | ||
|
||
now = datetime.now() | ||
date_and_time = now.strftime("%d.%m.%Y. %H:%M:%S") | ||
|
||
msg = MIMEMultipart() | ||
msg['From'] = sender | ||
msg['To'] = receiver | ||
msg['Subject'] = "Face detected" | ||
body = f"At {date_and_time}, camera #{self.camera_num} detected a face. Find attached image with the detected face." | ||
msg.attach(MIMEText(body, 'plain')) | ||
|
||
attachment = open("/tmp/face.jpg", "rb") | ||
p = MIMEBase('application', 'octet-stream') | ||
p.set_payload((attachment).read()) | ||
encoders.encode_base64(p) | ||
p.add_header('Content-Disposition', "attachment; filename= %s" % filename) | ||
msg.attach(p) | ||
|
||
username = "sissurvailance@gmail.com" | ||
password = "fw#22xCz" | ||
|
||
try: | ||
smtpObj = smtplib.SMTP('smtp.gmail.com', 587) | ||
smtpObj.ehlo() | ||
smtpObj.starttls() | ||
smtpObj.login(username, password) | ||
text = msg.as_string() | ||
smtpObj.sendmail(sender, receiver, text) | ||
smtpObj.quit() | ||
print(f"Email sent to {receiver}.") | ||
except Exception as e: | ||
print("Unable to send the email.") |