File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Animal:
2+
noise = "Rrrr"
3+
color = "Red"
4+
def make_noise(self): # this
5+
print(f"{self.noise}")
6+
def set_noise(self, new_noise):
7+
self.noise = new_noise
8+
def get_noise(self):
9+
return self.noise
10+
def set_noise(self, new_noise):
11+
self.noise = new_noise
12+
return self.noise
13+
def get_color(self):
14+
return self.color
15+
def set_color(self, new_color):
16+
self.color = new_color
17+
return self.color
18+
19+
20+
class Wolf(Animal):
21+
noise = "grrrr"
22+
23+
24+
class BabyWolf(Wolf):
25+
color = "yellow"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.mime.multipart import MIMEMultipart
4+
5+
from templates import Template
6+
# environment variables
7+
username = '[email protected]'
8+
password = '<your password>'
9+
10+
class Emailer():
11+
subject = ""
12+
context = {}
13+
to_emails = []
14+
has_html = False
15+
test_send = False
16+
from_email = 'Hungry Py <[email protected]>'
17+
template_html = None
18+
template_name = None
19+
20+
def __init__(self, subject="", template_name=None, context={}, template_html=None, to_emails=None, test_send=False):
21+
if template_name == None and template_name == None:
22+
raise Exception("You must set a template")
23+
assert isinstance(to_emails, list)
24+
self.to_emails = to_emails
25+
self.subject = subject
26+
if template_html != None:
27+
self.has_html = True
28+
self.template_html = template_html
29+
self.template_name = template_name
30+
self.context = context
31+
self.test_send = test_send
32+
33+
def format_msg(self):
34+
msg = MIMEMultipart('alternative')
35+
msg['From'] = self.from_email
36+
msg['To'] = ", ".join(self.to_emails)
37+
msg['Subject'] = self.subject
38+
39+
if self.template_name != None:
40+
tmpl_str = Template(template_name=self.template_name, context=self.context)
41+
txt_part = MIMEText(tmpl_str.render(), 'plain')
42+
print(txt_part)
43+
msg.attach(txt_part)
44+
if self.template_html != None:
45+
tmpl_str = Template(template_name=self.template_html, context=self.context)
46+
html_part = MIMEText(tmpl_str.render(), 'html')
47+
print(html_part)
48+
msg.attach(html_part)
49+
msg_str = msg.as_string()
50+
return msg_str
51+
52+
def send(self):
53+
msg = self.format_msg()
54+
# login to my smtp server
55+
did_send = False
56+
if not self.test_send:
57+
with smtplib.SMTP(host='smtp.gmail.com', port=587) as server:
58+
server.ehlo()
59+
server.starttls()
60+
server.login(username, password)
61+
try:
62+
server.sendmail(from_email, to_emails, msg_str)
63+
did_send = True
64+
except:
65+
did_send = False
66+
return did_send
67+
# with smtplib.SMTP() as server:
68+
# server.login()
69+
# pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
FILE_PATH = os.path.abspath(__file__)
4+
BASE_DIR = os.path.dirname(FILE_PATH)
5+
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
6+
7+
class Template:
8+
template_name = ""
9+
context = None
10+
11+
def __init__(self, template_name="", context=None, *args, **kwargs):
12+
self.template_name = template_name
13+
self.context = context
14+
15+
def get_template(self):
16+
template_path = os.path.join(TEMPLATE_DIR, self.template_name)
17+
if not os.path.exists(template_path):
18+
raise Exception("This path does not exist")
19+
template_string = ""
20+
with open(template_path, 'r') as f:
21+
template_string = f.read()
22+
return template_string
23+
24+
def render(self, context=None):
25+
render_ctx = context
26+
if self.context != None:
27+
render_ctx = self.context
28+
if not isinstance(render_ctx, dict):
29+
render_ctx = {}
30+
template_string = self.get_template()
31+
return template_string.format(**render_ctx) # {"name": "Justin"} -> name="Justin"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>Hello {name}!</p>
2+
3+
<p>This is cool.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello {name}!
2+
3+
This is cool.

0 commit comments

Comments
 (0)