My Django Project is sending the same email from different apps
I have a Django Project with two different apps: contact_us and booking. both are forms on different sections of the site.
when the form for booking request is being sent it saves the info to the table and it is sending a confirmation email... the problem is that the content of the email is from the contact_us view... it is sending from the same email account but the content should be different. From Contact i get the email with Contact info, i added logs everywhere to see where the error is but is not giving me any clues except that contact_us might be overwriting booking at some point
my settings are like this:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'host'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'email@email.com'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
public_pages views.py
def contact_us(request):
logger.debug("Request method: %s", request.method)
if request.method == 'POST':
logger.debug("Form data received: %s", request.POST)
form = ContactForm(request.POST)
if form.is_valid():
logger.debug("Form is valid")
contact = form.save() # Save the form data to the ContactUs model
logger.debug("Contact saved: %s", contact)
try:
# Send email to the user
send_mail(
'We Received Your Message',
f'Thank you for contacting us, {contact.name}. We will review your message shortly.',
settings.DEFAULT_FROM_EMAIL,
[contact.email],
fail_silently=False, # Set to False to see any errors
)
logger.debug("Email sent to: %s", contact.email)
messages.success(request, 'Your message has been sent successfully.')
except Exception as e:
# Log error
logger.error("Error sending email: %s", e)
messages.error(request, 'There was an error sending your message. Please try again later.')
return redirect('public_pages:contact_us')
else:
logger.debug("Form is invalid")
messages.error(request, 'Please correct the error below.')
else:
form = ContactForm()
return render(request, 'public_pages/contact_us.html', {'form': form})
booking_request views.py
def booking_request(request):
recipient_id = int(request.GET.get('recipient_id', 0))
logger.debug("Request method: %s", request.method)
if request.method == 'POST':
logger.debug("Form data received: %s", request.POST)
form = BookingRequestForm(request.POST)
if form.is_valid():
logger.debug("Form is valid")
booking_request = form.save(commit=False)
booking_request.recipient_id = recipient_id
# Deserialize the JSON field
booking_request.meal_courses = json.loads(form.cleaned_data['meal_courses'])
booking_request.save()
logger.debug("Booking request saved: %s", booking_request)
try:
# Send email to the user
send_mail(
f'Hey {booking_request.name}, we Received Your Booking Request',
f'Hello {booking_request.name},\n\n'
'Thank you for reaching out! We have received your booking request and will be working with different vendors to send you several options based on your preferences. '
'We will review the details of your request and get back to you shortly with more information. In the meantime, feel free to contact us if you have any questions.\n\n'
'Best regards,\n',
settings.DEFAULT_FROM_EMAIL,
[booking_request.email],
fail_silently=False,
)
logger.debug("Email sent to: %s", booking_request.email)
messages.success(request, 'Your message has been sent successfully.')
except Exception as e:
logger.error("Error sending email: %s", e, exc_info=True)
messages.error(request, 'There was an error sending your message. Please try again later.')
messages.success(request, 'Your booking request has been sent successfully.')
return redirect('booking_request:success')
else:
logger.debug("Form is invalid")
messages.error(request, 'Please correct the error below.')
else:
form = BookingRequestForm()
return render(request, 'booking_request/booking_request.html', {'form': form})