canaille-globuzma/canaille/mails.py

119 lines
3.5 KiB
Python
Raw Normal View History

2021-12-20 22:57:27 +00:00
from flask import current_app
from flask import url_for
2021-01-22 17:26:53 +00:00
from flask_babel import gettext as _
from flask_themer import render_template
2021-12-20 22:57:27 +00:00
from .apputils import logo
from .apputils import profile_hash
from .apputils import send_email
2021-01-22 17:26:53 +00:00
def send_password_reset_mail(user):
2021-05-24 15:43:15 +00:00
base_url = url_for("account.index", _external=True)
2021-01-22 17:26:53 +00:00
reset_url = url_for(
2021-05-24 15:43:15 +00:00
"account.reset",
2021-01-22 17:26:53 +00:00
uid=user.uid[0],
hash=profile_hash(
user.uid[0],
user.mail[0],
user.userPassword[0] if user.has_password() else "",
2021-01-22 17:26:53 +00:00
),
_external=True,
)
logo_cid, logo_filename, logo_raw = logo()
subject = _("Password reset on {website_name}").format(
website_name=current_app.config.get("NAME", reset_url)
)
text_body = render_template(
"mail/reset.txt",
site_name=current_app.config.get("NAME", reset_url),
site_url=base_url,
reset_url=reset_url,
)
html_body = render_template(
"mail/reset.html",
site_name=current_app.config.get("NAME", reset_url),
site_url=base_url,
reset_url=reset_url,
2021-12-20 22:57:27 +00:00
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
2021-01-22 17:26:53 +00:00
)
return send_email(
subject=subject,
recipient=user.mail,
text=text_body,
html=html_body,
attachements=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)
def send_password_initialization_mail(user):
2021-05-24 15:43:15 +00:00
base_url = url_for("account.index", _external=True)
2021-01-22 17:26:53 +00:00
reset_url = url_for(
2021-05-24 15:43:15 +00:00
"account.reset",
2021-01-22 17:26:53 +00:00
uid=user.uid[0],
hash=profile_hash(
user.uid[0],
user.mail[0],
user.userPassword[0] if user.has_password() else "",
2021-01-22 17:26:53 +00:00
),
_external=True,
)
logo_cid, logo_filename, logo_raw = logo()
subject = _("Password initialization on {website_name}").format(
website_name=current_app.config.get("NAME", reset_url)
)
text_body = render_template(
"mail/firstlogin.txt",
site_name=current_app.config.get("NAME", reset_url),
site_url=base_url,
reset_url=reset_url,
)
html_body = render_template(
"mail/firstlogin.html",
site_name=current_app.config.get("NAME", reset_url),
site_url=base_url,
reset_url=reset_url,
2021-12-20 22:57:27 +00:00
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
2021-01-22 17:26:53 +00:00
)
return send_email(
subject=subject,
recipient=user.mail,
text=text_body,
html=html_body,
attachements=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)
2021-12-01 11:19:28 +00:00
2021-12-01 11:26:14 +00:00
2021-12-01 11:19:28 +00:00
def send_invitation_mail(email, registration_url):
base_url = url_for("account.index", _external=True)
logo_cid, logo_filename, logo_raw = logo()
subject = _("You have been invited to create an account on {website_name}").format(
website_name=current_app.config.get("NAME", registration_url)
)
text_body = render_template(
2021-12-01 11:47:00 +00:00
"mail/invitation.txt",
2021-12-01 11:19:28 +00:00
site_name=current_app.config.get("NAME", registration_url),
site_url=base_url,
registration_url=registration_url,
)
html_body = render_template(
2021-12-01 11:47:00 +00:00
"mail/invitation.html",
2021-12-01 11:19:28 +00:00
site_name=current_app.config.get("NAME", registration_url),
site_url=base_url,
registration_url=registration_url,
2021-12-20 22:57:27 +00:00
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
2021-12-01 11:19:28 +00:00
)
return send_email(
subject=subject,
recipient=email,
text=text_body,
html=html_body,
attachements=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)