send email confirmation after lead creation

This commit is contained in:
Tobias Brunner 2025-03-10 14:33:40 +01:00
parent faa07a8b4d
commit b5584e1d8e
No known key found for this signature in database
4 changed files with 190 additions and 1 deletions

View file

@ -2,6 +2,10 @@ import logging
import time
from django.shortcuts import render, redirect
from django.contrib import messages
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
from hub.services.forms import LeadForm
from hub.services.odoo import OdooAPI
@ -130,6 +134,7 @@ def contact_form(request):
# Format the final message with all context
formatted_context = "<br>".join(context_info)
original_message = lead.message
lead.message = (
lead.message + "<br><br>" + formatted_context
if lead.message
@ -144,9 +149,45 @@ def contact_form(request):
odoo_lead_id = odoo.create_lead(lead, lead_title)
lead.odoo_lead_id = odoo_lead_id
lead.save()
logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")
# Send confirmation email
try:
# Prepare context for email template
email_context = {
"name": lead.name,
"email": lead.email,
"company": lead.company or "Not provided",
"phone": lead.phone or "Not provided",
"message": original_message,
"service": service_name or "General inquiry",
"provider": offering_name or "Not specified",
"plan": plan_name or "Not specified",
}
# Render email content from template
html_message = render_to_string(
"email/lead_confirmation.html", email_context
)
plain_message = render_to_string(
"email/lead_confirmation.txt", email_context
)
# Send the email
send_mail(
subject=f'Thank you for contacting Servala about {service_name or "our services"}',
message=plain_message,
html_message=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[lead.email],
fail_silently=False,
)
logger.info(f"Confirmation email sent to {lead.email}")
except Exception as e:
# Log error but don't stop the process
logger.error(
f"Failed to send confirmation email: {str(e)}", exc_info=True
)
# Redirect to thank you page
return redirect("services:thank_you")