Skip to content

Commit

Permalink
Change concept of mail template
Browse files Browse the repository at this point in the history
  • Loading branch information
Niepawowsky committed Jan 21, 2024
1 parent 2283be7 commit 91a6b20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
48 changes: 10 additions & 38 deletions mail_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@


class MailSender():
@staticmethod
# def send_data(func):
# def wrapper(*args, **kwargs):
# smtp_server = getenv('SMTP_SERVER')
# port = 465#('PORT')
# sender_email = getenv('EMAIL')
# password = getenv('PASSWORD')
#
# context = ssl.create_default_context()
#
# result = func(*args,
# smtp_server=smtp_server,
# port=port,
# sender_email=sender_email,
# password=password,
# context=context
# )
#
# return result
#
# return wrapper

@staticmethod
def send_data(func):
Expand All @@ -38,21 +17,8 @@ def wrapper(*args, **kwargs):
port = 465 # or int(getenv('PORT'))
sender_email = getenv('EMAIL')
password = getenv('PASSWORD')

context = ssl.create_default_context()

# # If the wrapped function already has these arguments, use them
# if 'smtp_server' in kwargs:
# smtp_server = kwargs['smtp_server']
# if 'port' in kwargs:
# port = kwargs['port']
# if 'sender_email' in kwargs:
# sender_email = kwargs['sender_email']
# if 'password' in kwargs:
# password = kwargs['password']
# if 'context' in kwargs:
# context = kwargs['context']

kwargs.update({
'smtp_server': smtp_server,
'port': port,
Expand All @@ -76,6 +42,9 @@ def __str__(self):
@send_data
def send_passed_return_date(self, *args, **kwargs): # smtp_server, port, sender_email, password, context):
print('Before connected to SMTP server')
sender = kwargs['sender_email']
password = kwargs['password']

try:
print('connected to SMTP server')
#
Expand All @@ -84,15 +53,18 @@ def send_passed_return_date(self, *args, **kwargs): # smtp_server, port, sender
today = datetime.datetime.today()
today.strftime('%Y-%m-%d')
result = borrower.return_at - today
result = str(result.days)
print(type(borrower.return_at))
mail = self.template.send_reminder_nearby().substitute(name=borrower.name, title=borrower.title,
return_at=borrower.return_at, result=str(result))
# mail = self.template.send_reminder_overdue().
mail = self.template.send_reminder_overdue().substitute(sender_mail=sender,name=borrower.name, title=borrower.title,
return_at=borrower.return_at, borrower_email=borrower.email, result=result)
#@TODO wysyla pustego maila
print('To jest mail:', mail)
print('Iterating threw self.borrowers')
server.login(kwargs['sender_email'], kwargs['password'])
server.login(sender, password)
print('logged in')
server.sendmail(kwargs['sender_email'], borrower.email, mail)
server.sendmail(from_addr=sender, to_addrs=borrower.email, msg=mail)
print('email sent')

except Exception as e:
print(f'Error: {e}')
49 changes: 40 additions & 9 deletions templates/template_render.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
from email.message import EmailMessage
from string import Template


class EmailTemplates():

@staticmethod
def send_reminder_overdue():
mssg = Template('$name, You have borrowed $title and return date passed $return_at\n Please return it imidietly\nBest Regards\nPawel')
return mssg
# mssg = Template('$name, You have borrowed $title and return date passed $return_at\n Please return it imidietly\nBest Regards\nPawel')
# return mssg
# message = EmailMessage()
# message.add_header('From', f'{sender}')
# message.add_header('To', f'{borrower.email}')
# message.add_header('Subject', 'Book return reminder')
# body = f'''
# Dear {borrower.name},
#
# You have borrowed {borrower.title} and return date passed {result}.
# Please return it imidietly.
#
# Best Regards
# Pawel
# '''
# message.set_content(body)
# message_as_string = message.as_string()

@staticmethod
def send_reminder_nearby():
mssg = Template('$name, You have borrowed $title and the return day is $return_at. Its about @result days from today.Please try not to overdue this dateBest Regards'
'Pawel')
return mssg
mssg = Template('''
... From: $sender_mail
... To: $borrower_email
... Subject: Book return reminder
... Dear $name, You have borrowed $title and return date passed $return_at
... Please return it imidietly.
... Best Regards
... Pawel ''')
return mssg
# @staticmethod
# def send_reminder_nearby():
# # mssg = Template('$name, You have borrowed $title and the return day is $return_at. Its about @result days from today.Please try not to overdue this dateBest Regards'
# # 'Pawel')
# # return mssg
# mssg = Template('''\
# ... From: $sender_mail
# ... Subject: Book return reminder
# ...
# ... $name, You have borrowed $title and return date will pass $return_at\n Its about $result days from today.Please try not to overdue this dateBest Regards ''')
# return mssg

0 comments on commit 91a6b20

Please sign in to comment.