-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
129 lines (104 loc) · 4.1 KB
/
mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
import getpass
import re
import os
import datetime
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
class MailHandler:
def __init__(self, recipient):
self.username, self.password = self.login_info()
self.recipient = recipient
self.server_type = self.smtp_server_type()
self.server = None
# Gather user's login info
def login_info(self, notifications=None):
email_user = None
email_pwd = None
while True:
if notifications == None:
use_email = raw_input(
"Do you wish to use email notifications? [Y/n]").strip()
if notifications or use_email == '' or use_email.lower() == 'y':
print("[INFO] Using email notifications.")
email_user = raw_input("Enter username: ").strip()
email_pwd = getpass.getpass(prompt='Enter password: ')
break
elif not notifications or use_email.lower() == 'n':
break
else:
print("Please respond with 'y' or 'n'.")
return email_user, email_pwd
# Define what smtp-server to use (outlook or gmail)
def smtp_server_type(self):
user = self.username
hotmail = "(.*[email protected])"
# Doesn't want emails
if not user:
return None
# Hotmail users match this pattern
elif re.match(hotmail, user):
print("[INFO] User is sending through hotmail smtp-server!")
return "hotmail"
# Gmail users
else:
print("[INFO] User is sending through gmail smtp-server!")
return "gmail"
def validate_login(self):
server_type = self.server_type
valid_smtp_server = None
while server_type:
valid_smtp_server = self.check_server()
# If not None, server is good to go
if valid_smtp_server:
print("[INFO] Login success! We have a valid smtp server!")
break
print("Please re-enter the login credentials for email.")
self.username, self.password = self.login_info(True)
server_type = self.smtp_server_type()
self.server = valid_smtp_server
# Validate login info
def check_server(self):
username = self.username
password = self.password
server_type = self.server_type
if server_type == "hotmail":
server = smtplib.SMTP('smtp.live.com', 587)
else:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
try:
server.login(username, password)
except smtplib.SMTPException:
print("[ERROR] Login failed, username or password isn't correct!")
return None
return server
# Construct email message
def construct_message(self, path):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
img_data = open(path, 'rb')
file_type = 'Video' if re.match(".*?.avi$", path) else 'Image'
file_size = os.path.getsize(path)
msg = MIMEMultipart()
textMessage = "Hello!\n\nPicture was taken with security camera. " \
"See the details of the picture below.\n\n\nTime: %s\nType: %s\n" \
"Size: %s Bytes\n\n\nImage is in the attachments!\n\n\n" \
"This message was sent automatically through sec-cam application." % (
now, file_type, file_size)
text = MIMEText(textMessage)
msg.attach(text)
image = MIMEImage(img_data.read())
msg.attach(image)
msg['Subject'] = 'Activity in security camera!'
return msg
# Send email to user, path gives the path to attachment (photo)
def send_mail(self, path):
server = self.server
msg = self.construct_message(path)
server.sendmail(self.username, self.recipient, msg.as_string())
print("[INFO] Email sent successfully!")