2026-01-27 08:24:22 +00:00
|
|
|
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
# See the LICENSE file for details.
|
|
|
|
|
|
2023-11-24 06:49:26 +00:00
|
|
|
# Python imports
|
2024-03-18 08:57:02 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
# Third party imports
|
|
|
|
|
from celery import shared_task
|
2023-11-24 06:49:26 +00:00
|
|
|
|
2022-11-29 21:17:42 +00:00
|
|
|
# Django imports
|
2024-05-08 17:31:20 +00:00
|
|
|
# Third party imports
|
2023-11-18 10:47:01 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives, get_connection
|
2022-11-29 21:17:42 +00:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
|
2023-11-18 10:47:01 +00:00
|
|
|
# Module imports
|
2023-11-29 15:03:37 +00:00
|
|
|
from plane.license.utils.instance_value import get_email_configuration
|
2026-02-16 19:14:52 +00:00
|
|
|
from plane.utils.email import generate_plain_text_from_html
|
2024-03-18 08:57:02 +00:00
|
|
|
from plane.utils.exception_logger import log_exception
|
2022-11-29 21:17:42 +00:00
|
|
|
|
2023-11-24 06:49:26 +00:00
|
|
|
|
2023-04-06 17:26:36 +00:00
|
|
|
@shared_task
|
2025-04-02 17:39:27 +00:00
|
|
|
def magic_link(email, key, token):
|
2022-11-29 21:17:42 +00:00
|
|
|
try:
|
2023-11-29 15:03:37 +00:00
|
|
|
(
|
|
|
|
|
EMAIL_HOST,
|
|
|
|
|
EMAIL_HOST_USER,
|
|
|
|
|
EMAIL_HOST_PASSWORD,
|
|
|
|
|
EMAIL_PORT,
|
|
|
|
|
EMAIL_USE_TLS,
|
2024-03-18 14:13:00 +00:00
|
|
|
EMAIL_USE_SSL,
|
2023-11-29 15:03:37 +00:00
|
|
|
EMAIL_FROM,
|
2023-12-07 09:21:27 +00:00
|
|
|
) = get_email_configuration()
|
2022-11-29 21:17:42 +00:00
|
|
|
|
2023-11-29 15:03:37 +00:00
|
|
|
# Send the mail
|
|
|
|
|
subject = f"Your unique Plane login code is {token}"
|
2023-11-30 08:00:13 +00:00
|
|
|
context = {"code": token, "email": email}
|
2023-11-29 15:03:37 +00:00
|
|
|
|
2024-11-21 12:12:44 +00:00
|
|
|
html_content = render_to_string("emails/auth/magic_signin.html", context)
|
2026-02-16 19:14:52 +00:00
|
|
|
text_content = generate_plain_text_from_html(html_content)
|
2022-11-29 21:17:42 +00:00
|
|
|
|
2023-11-18 10:47:01 +00:00
|
|
|
connection = get_connection(
|
2023-11-29 15:03:37 +00:00
|
|
|
host=EMAIL_HOST,
|
|
|
|
|
port=int(EMAIL_PORT),
|
|
|
|
|
username=EMAIL_HOST_USER,
|
|
|
|
|
password=EMAIL_HOST_PASSWORD,
|
2023-12-14 10:41:46 +00:00
|
|
|
use_tls=EMAIL_USE_TLS == "1",
|
2024-03-18 14:13:00 +00:00
|
|
|
use_ssl=EMAIL_USE_SSL == "1",
|
2023-11-18 10:47:01 +00:00
|
|
|
)
|
|
|
|
|
|
2023-11-24 06:49:26 +00:00
|
|
|
msg = EmailMultiAlternatives(
|
|
|
|
|
subject=subject,
|
|
|
|
|
body=text_content,
|
2023-11-29 15:03:37 +00:00
|
|
|
from_email=EMAIL_FROM,
|
2023-11-24 06:49:26 +00:00
|
|
|
to=[email],
|
|
|
|
|
connection=connection,
|
|
|
|
|
)
|
2022-11-29 21:17:42 +00:00
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
|
msg.send()
|
2025-05-09 08:53:23 +00:00
|
|
|
logging.getLogger("plane.worker").info("Email sent successfully.")
|
2022-11-29 21:17:42 +00:00
|
|
|
return
|
|
|
|
|
except Exception as e:
|
2024-03-18 08:57:02 +00:00
|
|
|
log_exception(e)
|
2022-11-29 21:17:42 +00:00
|
|
|
return
|