Skip to content

Commit 256c12d

Browse files
Day 13
1 parent 3937f93 commit 256c12d

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

Day 13/hungrypy/custom.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import smtplib
2+
3+
host = "smtp.gmail.com"
4+
port = 587
5+
username = "[email protected]"
6+
password = "iamhungry2016"
7+
from_email = username
8+
to_list = ["[email protected]"]
9+
10+
email_conn = smtplib.SMTP(host, port)
11+
email_conn.ehlo()
12+
email_conn.starttls()
13+
email_conn.login(username, password)
14+
email_conn.sendmail(from_email, to_list, "Hello there this is an email message")
15+
email_conn.quit()
16+
17+
18+
from smtplib import SMTP
19+
20+
21+
ABC = SMTP(host, port)
22+
ABC.ehlo()
23+
ABC.starttls()
24+
ABC.login(username, password)
25+
ABC.sendmail(from_email, to_list, "Hello there this is an email message")
26+
ABC.quit()
27+
28+
29+
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
30+
31+
32+
pass_wrong = SMTP(host, port)
33+
pass_wrong.ehlo()
34+
pass_wrong.starttls()
35+
try:
36+
pass_wrong.login(username, "wrong_password")
37+
pass_wrong.sendmail(from_email, to_list, "Hello there this is an email message")
38+
except SMTPAuthenticationError:
39+
print("Could not login")
40+
except:
41+
print("an error occured")
42+
43+
pass_wrong.quit()
44+
45+
46+

Day 13/hungrypy/html_format_email.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from email.mime.multipart import MIMEMultipart
2+
from email.mime.text import MIMEText
3+
import smtplib
4+
5+
6+
host = "smtp.gmail.com"
7+
port = 587
8+
username = "[email protected]"
9+
password = "iamhungry2016"
10+
from_email = username
11+
to_list = ["[email protected]"]
12+
13+
try:
14+
email_conn = smtplib.SMTP(host, port)
15+
email_conn.ehlo()
16+
email_conn.starttls()
17+
email_conn.login(username, password)
18+
the_msg = MIMEMultipart("alternative")
19+
the_msg['Subject'] = "Hello there!"
20+
the_msg["From"] = from_email
21+
#the_msg["To"] = to_list[0]
22+
plain_txt = "Testing the message"
23+
html_txt = """\
24+
<html>
25+
<head></head>
26+
<body>
27+
<p>Hey!<br>
28+
Testing this email <b>message</b>. Made by <a href='http://joincfe.com'>Team CFE</a>.
29+
</p>
30+
</body>
31+
</html>
32+
"""
33+
part_1 = MIMEText(plain_txt, 'plain')
34+
part_2 = MIMEText(html_txt, "html")
35+
the_msg.attach(part_1)
36+
the_msg.attach(part_2)
37+
email_conn.sendmail(from_email, to_list, the_msg.as_string())
38+
email_conn.quit()
39+
except smtplib.SMTPException:
40+
print("error sending message")
41+
42+
43+
44+
45+
46+
47+

Day 13/hungrypy/message_users.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import datetime
2+
from email.mime.multipart import MIMEMultipart
3+
from email.mime.text import MIMEText
4+
import smtplib
5+
6+
host = "smtp.gmail.com"
7+
port = 587
8+
username = "[email protected]"
9+
password = "iamhungry2016"
10+
from_email = username
11+
to_list = ["[email protected]"]
12+
13+
14+
15+
class MessageUser():
16+
user_details = []
17+
messages = []
18+
email_messages = []
19+
base_message = """Hi {name}!
20+
21+
Thank you for the purchase on {date}.
22+
We hope you are exicted about using it. Just as a
23+
reminder the purcase total was ${total}.
24+
Have a great one!
25+
26+
Team CFE
27+
"""
28+
def add_user(self, name, amount, email=None):
29+
name = name[0].upper() + name[1:].lower()
30+
amount = "%.2f" %(amount)
31+
detail = {
32+
"name": name,
33+
"amount": amount,
34+
}
35+
today = datetime.date.today()
36+
date_text = '{today.month}/{today.day}/{today.year}'.format(today=today)
37+
detail['date'] = date_text
38+
if email is not None: # if email != None
39+
detail["email"] = email
40+
self.user_details.append(detail)
41+
def get_details(self):
42+
return self.user_details
43+
def make_messages(self):
44+
if len(self.user_details) > 0:
45+
for detail in self.get_details():
46+
name = detail["name"]
47+
amount = detail["amount"]
48+
date = detail["date"]
49+
message = self.base_message
50+
new_msg = message.format(
51+
name=name,
52+
date=date,
53+
total=amount
54+
)
55+
user_email = detail.get("email")
56+
if user_email:
57+
user_data = {
58+
"email": user_email,
59+
"message": new_msg
60+
}
61+
self.email_messages.append(user_data)
62+
else:
63+
self.messages.append(new_msg)
64+
return self.messages
65+
return []
66+
def send_email(self):
67+
self.make_messages()
68+
if len(self.email_messages) > 0:
69+
for detail in self.email_messages:
70+
user_email = detail['email']
71+
user_message = detail['message']
72+
try:
73+
email_conn = smtplib.SMTP(host, port)
74+
email_conn.ehlo()
75+
email_conn.starttls()
76+
email_conn.login(username, password)
77+
the_msg = MIMEMultipart("alternative")
78+
the_msg['Subject'] = "Billing Update!"
79+
the_msg["From"] = from_email
80+
the_msg["To"] = user_email
81+
part_1 = MIMEText(user_message, 'plain')
82+
the_msg.attach(part_1)
83+
email_conn.sendmail(from_email, [user_email], the_msg.as_string())
84+
email_conn.quit()
85+
except smtplib.SMTPException:
86+
print("error sending message")
87+
return True
88+
return False
89+
90+
91+
obj = MessageUser()
92+
obj.add_user("Justin", 123.32, email='[email protected]')
93+
obj.add_user("jOhn", 94.23, email='[email protected]')
94+
obj.add_user("Sean", 93.23, email='[email protected]')
95+
obj.add_user("Emilee", 193.23, email='[email protected]')
96+
obj.add_user("Marie", 13.23, email='[email protected]')
97+
obj.get_details()
98+
99+
obj.send_email()

Day 13/hungrypy/templates.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
3+
4+
def get_template_path(path):
5+
file_path = os.path.join(os.getcwd(), path)
6+
if not os.path.isfile(file_path):
7+
raise Exception("This is not a valid template path %s"%(file_path))
8+
return file_path
9+
10+
11+
def get_template(path):
12+
file_path = get_template_path(path)
13+
return open(file_path).read()
14+
15+
16+
def render_context(template_string, context):
17+
return template_string.format(**context)
18+
19+
file_ = 'templates/email_message.txt'
20+
file_html = 'templates/email_message.html'
21+
template = get_template(file_)
22+
template_html = get_template(file_html)
23+
context = {
24+
"name": "Justin",
25+
"date": None,
26+
"total": None
27+
}
28+
29+
print(render_context(template, context))
30+
print(render_context(template_html, context))
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
"""
41+
/abc/ad/file.txt
42+
\hi\this\ois\a\file.txt
43+
open("\hi\this\ois\a\file.txt").read()
44+
open("/abc/ad/file.txt").read()
45+
"""
46+
47+
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head></head>
3+
<body>
4+
<p>Hey {name}!<br>
5+
{date} total: {total}<br/>
6+
Testing this email <b>message</b>. Made by <a href='http://joincfe.com'>Team CFE</a>.
7+
</p>
8+
</body>
9+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Hello {name}!
2+
3+
Thank you for the purchase on.
4+
We hope you are exicted about using it. Just as a
5+
reminder the purchase total was ${total}.
6+
Have a great one!
7+
8+
Team CFE

0 commit comments

Comments
 (0)