Skip to content
Snippets Groups Projects
Commit a5c50780 authored by Václav Volhejn's avatar Václav Volhejn
Browse files

Funkce na posílání emailu na reset hesla

parent 6593a567
No related branches found
No related tags found
1 merge request!3Posílání emailů na obnovu hesla
This commit is part of merge request !3. Comments created here will be created in the context of that merge request.
......@@ -6,3 +6,5 @@ SECRET_KEY = "FIXME"
SESSION_COOKIE_PATH = '/'
SESSION_COOKIE_NAME = 'mo_session'
MAIL_FROM = "mo-submit@matfyz.cz" # FIXME
\ No newline at end of file
# Různé
import mo.db as db
from typing import Any, Optional
import email.message
import email.headerregistry
import datetime
import subprocess
import textwrap
import mo.db as db
import config
current_log_user: Optional[db.User] = None
......@@ -14,3 +21,44 @@ def log(type: db.LogType, what: int, details: Any):
details=details,
)
db.get_session().add(entry)
def send_password_reset_email(user: db.User, link: str):
if not hasattr(config, 'MAIL_FROM'):
raise RuntimeError('V configu chybí pole MAIL_FROM.')
msg = email.message.EmailMessage()
msg['From'] = email.headerregistry.Address(
display_name='MO Submit', addr_spec=config.MAIL_FROM
)
msg['To'] = [
email.headerregistry.Address(
display_name='{} {}'.format(user.first_name, user.last_name),
addr_spec=user.email,
)
]
msg['Subject'] = f'MO Submit – obnova hesla'
msg['Date'] = datetime.datetime.now()
body = textwrap.dedent('''
Pro obnovení hesla pro váš účet na MO Submit klikněte sem: {}
Váš MO Submit
'''.format(link))
msg.set_content(body, cte='quoted-printable')
sm = subprocess.Popen(
[
'/usr/sbin/sendmail',
'-oi',
'-f',
config.MAIL_FROM,
user.email,
],
stdin=subprocess.PIPE,
)
sm.communicate(msg.as_bytes())
if sm.returncode != 0:
raise RuntimeError('Sendmail failed with return code {}'.format(sm.returncode))
......@@ -4,6 +4,7 @@ import wtforms
import wtforms.validators as validators
from sqlalchemy.orm import joinedload
import mo.util
import mo.db as db
import mo.rights
import mo.users
......@@ -37,7 +38,12 @@ def login():
token = mo.users.ask_reset_password(user)
link = url_for('reset', token=token)
db.get_session().commit()
# FIXME: Poslat e-mail
try:
mo.util.send_password_reset_email(user, link)
except RuntimeError as e:
app.logger.error('Login: problém při posílání emailu: {}'.format(e))
app.logger.info('Link: %s', link)
return render_template('reset.html')
elif not form.passwd.data or not mo.users.check_password(user, form.passwd.data):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment