forked from jmarkowski/secret-santa-mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter.py
39 lines (29 loc) · 1.18 KB
/
letter.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
import smtplib
import config
from santa import Santa
class Letter(object):
def __init__(self, from_name, from_email, subject, body):
self.from_name = from_name
self.from_email = from_email
self.subject = subject
self.body = body
def get_email_message(self, santa):
message = \
f'From: {self.from_name} <{self.from_email}>\n' \
f'To: {santa.name} <{santa.email}>\n' \
f'Subject: {self.subject}\n\n' \
f'{self.body}\n'
message = message.replace('{santa}', santa.name)
message = message.replace('{recipient}', santa.recipient.name)
return message
def send(self, santa):
message = self.get_email_message(santa)
try:
server = smtplib.SMTP(config.smtp_host, config.smtp_port)
server.starttls()
server.login(config.smtp_user, config.smtp_pass)
server.sendmail(self.from_email, [santa.email], message.encode('utf-8'))
server.close()
print('Successfully mailed letter to {}!'.format(santa.name))
except Exception as e:
print('Error: Failed to mail letter: {}'.format(e))