-
Notifications
You must be signed in to change notification settings - Fork 0
/
pat.py
executable file
·107 lines (84 loc) · 2.47 KB
/
pat.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
#!/usr/bin/python3
# All software written by Tomas. (https://github.com/shelbenheimer)
import os
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
OUTLOOK_SERVER = 'smtp-mail.outlook.com'
GMAIL_SERVER = 'smtp.gmail.com'
OUTLOOK_PORT = 587
GMAIL_PORT = 587
DEFAULT_SERVER = GMAIL_SERVER
DEFAULT_PORT = GMAIL_PORT
DEFAULT_TIMEOUT = 15
DEFAULT_TLS = True
BANNER = "Software written by Tomas. Available on GitHub. (https://github.com/shelbenheimer)"
REQUIRED = [ '-s', '-r', '-p', '-t', '-f' ]
class Post:
def __init__(self, server, port, timeout, tls):
self.server = server
self.port = port
self.timeout = timeout
self.tls = tls
self.connection = None
self.subject = None
self.sender = None
self.recipient = None
self.password = None
self.content = None
def ParseArgs(self, args):
for arg in range(0, len(args)):
match args[arg]:
case '-s':
self.sender = args[arg + 1]
case '-r':
self.recipient = args[arg + 1]
case '-p':
self.password = args[arg + 1]
case '-t':
self.subject = args[arg + 1]
case '-f':
self.content = self.ParseHTML(args[arg + 1])
def ParseHTML(self, path):
if not os.path.exists(path):
print("Path to file not found.")
sys.exit()
buffer = []
with open(path, 'r', encoding="utf8") as file:
for line in file:
buffer.append(line)
return "".join(buffer)
return ""
def ConstructMail(self):
message = MIMEMultipart()
message['From'] = self.sender
message['To'] = self.recipient
message['Subject'] = self.subject
message.attach(MIMEText(self.content, 'html'))
return message.as_string()
def EstablishConnection(self):
self.connection = smtplib.SMTP(self.server, self.port, None, self.timeout, None)
if self.tls: self.connection.starttls()
self.connection.login(self.sender, self.password)
def SendMail(self, mail):
if not self.connection: return
self.connection.sendmail(self.sender, self.recipient, mail)
self.connection.quit()
def CheckRequired(args, required):
for arg in required:
if arg not in args: return False
return True
try:
post = Post(DEFAULT_SERVER, DEFAULT_PORT, DEFAULT_TIMEOUT, DEFAULT_TLS)
if not CheckRequired(sys.argv, REQUIRED):
print("Flag criteria not met.")
sys.exit()
print(BANNER)
post.ParseArgs(sys.argv)
post.EstablishConnection()
mail = post.ConstructMail()
post.SendMail(mail)
print("Sent!")
except KeyboardInterrupt:
print("Caught interruption. Exiting gracefully.")