2024-03-15 18:58:06 +00:00
|
|
|
from flask import current_app
|
|
|
|
from flask import url_for
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
from canaille.app import build_hash
|
2023-09-01 08:46:56 +00:00
|
|
|
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
|
2023-08-16 15:14:11 +00:00
|
|
|
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):
|
2023-08-14 11:52:24 +00:00
|
|
|
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(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2023-01-22 11:48:15 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/test.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
site_name=current_app.config["CANAILLE"]["NAME"],
|
2023-01-22 11:48:15 +00:00
|
|
|
site_url=base_url,
|
|
|
|
)
|
|
|
|
html_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/test.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
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,
|
2023-07-01 17:56:13 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-29 15:47:01 +00:00
|
|
|
def send_password_reset_mail(user, mail):
|
2023-08-14 11:52:24 +00:00
|
|
|
base_url = url_for("core.account.index", _external=True)
|
2021-01-22 17:26:53 +00:00
|
|
|
reset_url = url_for(
|
2023-08-14 14:15:41 +00:00
|
|
|
"core.auth.reset",
|
2023-06-28 15:56:49 +00:00
|
|
|
user=user,
|
2023-07-20 16:43:28 +00:00
|
|
|
hash=build_hash(
|
2023-06-27 15:41:00 +00:00
|
|
|
user.identifier,
|
2023-06-29 15:47:01 +00:00
|
|
|
mail,
|
2023-11-28 14:51:14 +00:00
|
|
|
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(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2021-01-22 17:26:53 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/reset.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
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(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/reset.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
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,
|
2023-07-01 17:56:13 +00:00
|
|
|
title=subject,
|
2021-01-22 17:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return send_email(
|
|
|
|
subject=subject,
|
2023-11-28 16:59:50 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-29 15:47:01 +00:00
|
|
|
def send_password_initialization_mail(user, email):
|
2023-08-14 11:52:24 +00:00
|
|
|
base_url = url_for("core.account.index", _external=True)
|
2021-01-22 17:26:53 +00:00
|
|
|
reset_url = url_for(
|
2023-08-14 14:15:41 +00:00
|
|
|
"core.auth.reset",
|
2023-06-28 15:56:49 +00:00
|
|
|
user=user,
|
2023-07-20 16:43:28 +00:00
|
|
|
hash=build_hash(
|
2023-06-27 15:41:00 +00:00
|
|
|
user.identifier,
|
2023-06-29 15:47:01 +00:00
|
|
|
email,
|
2023-11-28 14:51:14 +00:00
|
|
|
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(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2021-01-22 17:26:53 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/firstlogin.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
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(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/firstlogin.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
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,
|
2023-07-01 17:56:13 +00:00
|
|
|
title=subject,
|
2021-01-22 17:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return send_email(
|
|
|
|
subject=subject,
|
2023-11-28 16:59:50 +00:00
|
|
|
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):
|
2023-08-14 11:52:24 +00:00
|
|
|
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(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2021-12-01 11:19:28 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/invitation.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
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(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/invitation.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
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,
|
2023-07-01 17:56:13 +00:00
|
|
|
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):
|
2023-08-14 11:52:24 +00:00
|
|
|
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(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2023-07-20 16:43:28 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/email-confirmation.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
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(
|
2023-08-14 13:28:20 +00:00
|
|
|
"mails/email-confirmation.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
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
|
|
|
)
|
2023-05-29 13:54:47 +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)
|
2023-05-29 13:54:47 +00:00
|
|
|
logo_cid, logo_filename, logo_raw = logo()
|
|
|
|
|
|
|
|
subject = _("Continue your registration on {website_name}").format(
|
2023-12-18 17:06:03 +00:00
|
|
|
website_name=current_app.config["CANAILLE"]["NAME"]
|
2023-05-29 13:54:47 +00:00
|
|
|
)
|
|
|
|
text_body = render_template(
|
2023-08-15 14:17:19 +00:00
|
|
|
"mails/registration.txt",
|
2023-12-18 17:06:03 +00:00
|
|
|
site_name=current_app.config["CANAILLE"]["NAME"],
|
2023-05-29 13:54:47 +00:00
|
|
|
site_url=base_url,
|
|
|
|
registration_url=registration_url,
|
|
|
|
)
|
|
|
|
html_body = render_template(
|
2023-08-15 14:17:19 +00:00
|
|
|
"mails/registration.html",
|
2023-12-18 17:06:03 +00:00
|
|
|
site_name=current_app.config["CANAILLE"]["NAME"],
|
2023-05-29 13:54:47 +00:00
|
|
|
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,
|
2023-05-29 13:54:47 +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-05-29 13:54:47 +00:00
|
|
|
)
|