canaille-globuzma/canaille/core/mails.py

283 lines
8.7 KiB
Python
Raw Normal View History

from flask import current_app
from flask import url_for
2023-07-20 16:43:28 +00:00
from canaille.app import build_hash
from canaille.app.i18n import gettext as _
2023-04-09 13:52:55 +00:00
from canaille.app.mails import logo
from canaille.app.mails import send_email
from canaille.app.themes import render_template
2021-12-20 22:57:27 +00:00
2021-01-22 17:26:53 +00:00
2023-01-22 11:48:15 +00:00
def send_test_mail(email):
base_url = url_for("core.account.index", _external=True)
2023-01-22 11:48:15 +00:00
logo_cid, logo_filename, logo_raw = logo()
subject = _("Test email from {website_name}").format(
website_name=current_app.config["CANAILLE"]["NAME"]
2023-01-22 11:48:15 +00:00
)
text_body = render_template(
"core/mails/test.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
2023-01-22 11:48:15 +00:00
site_url=base_url,
)
html_body = render_template(
"core/mails/test.html",
site_name=current_app.config["CANAILLE"]["NAME"],
2023-01-22 11:48:15 +00:00
site_url=base_url,
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
title=subject,
2023-01-22 11:48:15 +00:00
)
return send_email(
subject=subject,
recipient=email,
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
2023-01-22 11:48:15 +00:00
)
def send_password_reset_mail(user, mail):
base_url = url_for("core.account.index", _external=True)
2021-01-22 17:26:53 +00:00
reset_url = url_for(
"core.auth.reset",
2023-06-28 15:56:49 +00:00
user=user,
2023-07-20 16:43:28 +00:00
hash=build_hash(
user.identifier,
mail,
user.password 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["CANAILLE"]["NAME"]
2021-01-22 17:26:53 +00:00
)
text_body = render_template(
"core/mails/reset.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-01-22 17:26:53 +00:00
site_url=base_url,
reset_url=reset_url,
)
html_body = render_template(
"core/mails/reset.html",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-01-22 17:26:53 +00:00
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,
title=subject,
2021-01-22 17:26:53 +00:00
)
return send_email(
subject=subject,
recipient=mail,
2021-01-22 17:26:53 +00:00
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
2021-01-22 17:26:53 +00:00
)
def send_password_initialization_mail(user, email):
base_url = url_for("core.account.index", _external=True)
2021-01-22 17:26:53 +00:00
reset_url = url_for(
"core.auth.reset",
2023-06-28 15:56:49 +00:00
user=user,
2023-07-20 16:43:28 +00:00
hash=build_hash(
user.identifier,
email,
user.password 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["CANAILLE"]["NAME"]
2021-01-22 17:26:53 +00:00
)
text_body = render_template(
"core/mails/firstlogin.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-01-22 17:26:53 +00:00
site_url=base_url,
reset_url=reset_url,
)
html_body = render_template(
"core/mails/firstlogin.html",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-01-22 17:26:53 +00:00
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,
title=subject,
2021-01-22 17:26:53 +00:00
)
return send_email(
subject=subject,
recipient=email,
2021-01-22 17:26:53 +00:00
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
2021-01-22 17:26:53 +00:00
)
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("core.account.index", _external=True)
2021-12-01 11:19:28 +00:00
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["CANAILLE"]["NAME"]
2021-12-01 11:19:28 +00:00
)
text_body = render_template(
"core/mails/invitation.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-12-01 11:19:28 +00:00
site_url=base_url,
registration_url=registration_url,
)
html_body = render_template(
"core/mails/invitation.html",
site_name=current_app.config["CANAILLE"]["NAME"],
2021-12-01 11:19:28 +00:00
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,
title=subject,
2021-12-01 11:19:28 +00:00
)
return send_email(
subject=subject,
recipient=email,
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
2021-12-01 11:19:28 +00:00
)
2023-07-20 16:43:28 +00:00
def send_confirmation_email(email, confirmation_url):
base_url = url_for("core.account.index", _external=True)
2023-07-20 16:43:28 +00:00
logo_cid, logo_filename, logo_raw = logo()
subject = _("Confirm your address email on {website_name}").format(
website_name=current_app.config["CANAILLE"]["NAME"]
2023-07-20 16:43:28 +00:00
)
text_body = render_template(
"core/mails/email-confirmation.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
2023-07-20 16:43:28 +00:00
site_url=base_url,
confirmation_url=confirmation_url,
)
html_body = render_template(
"core/mails/email-confirmation.html",
site_name=current_app.config["CANAILLE"]["NAME"],
2023-07-20 16:43:28 +00:00
site_url=base_url,
confirmation_url=confirmation_url,
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
title=subject,
2021-12-01 11:19:28 +00:00
)
return send_email(
subject=subject,
recipient=email,
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
2021-12-01 11:19:28 +00:00
)
def send_registration_mail(email, registration_url):
2023-08-15 14:17:19 +00:00
base_url = url_for("core.account.index", _external=True)
logo_cid, logo_filename, logo_raw = logo()
subject = _("Continue your registration on {website_name}").format(
website_name=current_app.config["CANAILLE"]["NAME"]
)
text_body = render_template(
"core/mails/registration.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
registration_url=registration_url,
)
html_body = render_template(
"core/mails/registration.html",
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
registration_url=registration_url,
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
2023-08-15 14:17:19 +00:00
title=subject,
)
return send_email(
subject=subject,
recipient=email,
text=text_body,
html=html_body,
2024-09-11 07:33:42 +00:00
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)
def send_compromised_password_check_failure_mail(
check_password_url, user_name, user_email, hashed_password, admin_email
):
base_url = url_for("core.account.index", _external=True)
logo_cid, logo_filename, logo_raw = logo()
subject = _("Compromised password check failure on {website_name}").format(
website_name=current_app.config["CANAILLE"]["NAME"]
)
text_body = render_template(
"core/mails/compromised_password_check_failure.txt",
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
check_password_url=check_password_url,
user_name=user_name,
user_email=user_email,
hashed_password=hashed_password,
)
html_body = render_template(
"core/mails/compromised_password_check_failure.html",
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
check_password_url=check_password_url,
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
title=subject,
user_name=user_name,
user_email=user_email,
hashed_password=hashed_password,
)
return send_email(
subject=subject,
recipient=admin_email,
text=text_body,
html=html_body,
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)
2024-11-18 13:16:38 +00:00
def send_one_time_password_mail(mail, otp):
base_url = url_for("core.account.index", _external=True)
logo_cid, logo_filename, logo_raw = logo()
subject = _("One-time password authentication on {website_name}").format(
website_name=current_app.config["CANAILLE"]["NAME"]
)
text_body = render_template(
"core/mails/email_otp.txt",
2024-11-18 13:16:38 +00:00
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
otp=otp,
)
html_body = render_template(
"core/mails/email_otp.html",
2024-11-18 13:16:38 +00:00
site_name=current_app.config["CANAILLE"]["NAME"],
site_url=base_url,
otp=otp,
logo=f"cid:{logo_cid[1:-1]}" if logo_cid else None,
title=subject,
)
return send_email(
subject=subject,
recipient=mail,
text=text_body,
html=html_body,
attachments=[(logo_cid, logo_filename, logo_raw)] if logo_filename else None,
)