Skip to content

Commit 95a3f60

Browse files
authored
fix: Disable mail system (#316)
* fix: Disable mail system
1 parent 33c8dc8 commit 95a3f60

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ Enter http://127.0.0.1:8080, and the initial credentials should appear in your t
7979

8080
After logging in, use [localhost admin](https://127.0.0.1:8080/admin) to modify entries in the database.
8181

82+
In case you want to enable the mail system:
83+
84+
1. Insert your mail details in the configuration file.
85+
2. Delete the `DISABLE_MAIL` line.
86+
8287

8388
## Code modification check list
8489
### Run flake8

lms/lmsweb/config.py.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ MAIL_USE_TLS = False
2222
MAIL_USERNAME = 'username@gmail.com'
2323
MAIL_PASSWORD = 'password'
2424
MAIL_DEFAULT_SENDER = 'username@gmail.com'
25+
DISABLE_MAIL = True # On production, delete this line!
2526

2627
# ADMIN PANEL
2728
FLASK_ADMIN_FLUID_LAYOUT = True

lms/utils/mail.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
from functools import wraps
2+
13
from flask import url_for
24
from flask_babel import gettext as _ # type: ignore
35
from flask_mail import Message # type: ignore
46

57
from lms.lmsdb.models import User
6-
from lms.lmsweb import config, webmail
8+
from lms.lmsweb import config, webapp, webmail
79
from lms.models.users import generate_user_token
810

911

12+
def send_message(func):
13+
@wraps(func)
14+
def wrapper(*args, **kwargs):
15+
msg = func(*args, **kwargs)
16+
if not webapp.config.get('DISABLE_MAIL'):
17+
webmail.send(msg)
18+
19+
return wrapper
20+
21+
22+
@send_message
1023
def send_confirmation_mail(user: User) -> Message:
1124
token = generate_user_token(user)
1225
subject = _('מייל אימות - %(site_name)s', site_name=config.SITE_NAME)
@@ -18,9 +31,10 @@ def send_confirmation_mail(user: User) -> Message:
1831
'שלום %(fullname)s,\nלינק האימות שלך למערכת הוא: %(link)s',
1932
fullname=user.fullname, link=link,
2033
)
21-
webmail.send(msg)
34+
return msg
2235

2336

37+
@send_message
2438
def send_reset_password_mail(user: User) -> Message:
2539
token = generate_user_token(user)
2640
subject = _('מייל איפוס סיסמה - %(site_name)s', site_name=config.SITE_NAME)
@@ -32,9 +46,10 @@ def send_reset_password_mail(user: User) -> Message:
3246
'שלום %(fullname)s,\nלינק לצורך איפוס הסיסמה שלך הוא: %(link)s',
3347
fullname=user.fullname, link=link,
3448
)
35-
webmail.send(msg)
49+
return msg
3650

3751

52+
@send_message
3853
def send_change_password_mail(user: User) -> Message:
3954
subject = _('שינוי סיסמה - %(site_name)s', site_name=config.SITE_NAME)
4055
msg = Message(subject, recipients=[user.mail_address])
@@ -45,4 +60,4 @@ def send_change_password_mail(user: User) -> Message:
4560
fullname=user.fullname, site_name=config.SITE_NAME,
4661
site_mail=config.MAIL_DEFAULT_SENDER,
4762
)
48-
webmail.send(msg)
63+
return msg

0 commit comments

Comments
 (0)