from django.core.mail import send_mail
from django.urls import reverse
from .models import EmailConfirmation
import uuid
from django.conf import settings
from django.utils.timezone import now
from backend.core.models import Document, Organization
from django.core.mail import EmailMultiAlternatives
from django.utils.html import format_html
from backend.core.utils import generate_first_page_image
from email.mime.image import MIMEImage
site_domain = settings.SITE_DOMAIN
def send_confirmation_email(email):
confirmation, created = EmailConfirmation.objects.get_or_create(email=email)
if not created:
confirmation.uuid = uuid.uuid4()
confirmation.created_at = now()
confirmation.save()
confirmation_link = f"{site_domain}{reverse('confirm_email', args=[confirmation.uuid])}"
content_html = format_html(
"""
Dear user,
To start generating your document, please confirm your email address by clicking the button below:
Confirm Email
If you did not request this, you can safely ignore this message.
Best regards,
Risklet Team
""",
confirmation_link
)
html_message = render_email_template(content_html, title="Confirm Your Email")
msg = EmailMultiAlternatives(
subject="Confirm your email to start document generation",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email]
)
msg.content_subtype = "html"
msg.send()
def send_payment_email(email):
payment_link = f"{site_domain}{reverse('core:payment_page')}?email={email}"
content_html = format_html(
"""
Dear customer,
Your document is almost ready. Please complete the payment process to unlock the final version.
Proceed to Payment
If you have already completed the payment, you can ignore this message.
Best regards,
Risklet Team
""",
payment_link,
)
html_message = render_email_template(content_html, title="Complete Your Payment")
msg = EmailMultiAlternatives(
subject="Complete your payment to finish document generation",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email],
)
msg.content_subtype = "html"
msg.send()
def send_documet_to_expert(email, document):
document_link = f"{site_domain}{reverse('core:document', args=[document.id])}"
edit_link = f"{site_domain}{reverse('admin:core_document_change', args=[document.id])}"
content_html = format_html(
"""
Hello,
A new document has been assigned to you for expert analysis. Please review the details below:
View Document
Edit in Admin
Thank you for your contribution.
Best regards,
Risklet Team
""",
document_link,
edit_link,
)
html_message = render_email_template(content_html, title="New Expert Assignment")
msg = EmailMultiAlternatives(
subject="Document assigned for expert analysis",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email],
)
msg.content_subtype = "html"
msg.send()
def send_document_to_reviewer(email, document):
document_link = f"{site_domain}{reverse('core:document', args=[document.id])}"
edit_link = f"{site_domain}{reverse('admin:core_document_change', args=[document.id])}"
content_html = format_html(
"""
Hello,
We encountered an issue while generating the document. Please review and complete the document before sending it to the customer.
View Document
Edit in Admin
Once you finish, save the document and share it with the customer.
Thank you for your help.
Risklet Team
""",
document_link,
edit_link,
)
html_message = render_email_template(content_html, title="Document Review Required")
msg = EmailMultiAlternatives(
subject="Document review required",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email],
)
msg.content_subtype = "html"
msg.send()
def send_document_email(email, document_link, document):
image_io = generate_first_page_image(document)
content_html = format_html(
"""
Your document is ready. Click the preview below to open the full PDF:
If you have any questions, feel free to reach out.
Best regards,
Risklet Team
""",
document_link,
)
html_message = render_email_template(content_html, title="Your Document is Ready")
msg = EmailMultiAlternatives(
subject="Your document is ready",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email],
)
msg.content_subtype = "html"
image_attachment = MIMEImage(image_io.getvalue(), "image/jpeg")
image_attachment.add_header("Content-ID", "")
msg.attach(image_attachment)
msg.send()
def render_email_template(content_html, title = 'Risklet Notifications'):
logo_url = f"https://risklet.com/static/img/risklet-icon.png"
return format_html(
"""
Risklet
{}
{}
Risklet — Smart cybersecurity risk assessment
""",
logo_url,
title,
content_html,
)