Why the email is not sending into my other email with that created object?
My idea is when someone submits the form and object created and my email verified in the settings with my app password sends an email to my other work email.
My scenario is: Scenario: Omar Haji has a personal website with a "Contact Me" form. A visitor, named Sarah, visits the website and wants to get in touch. She fills out the form with the following information:
Name: Sarah Alami Email: sarah@example.com Subject: Inquiry about Collaboration Message: "Hi Omar, I came across your portfolio, and I am interested in collaborating on a web development project. Let’s connect!" When Sarah clicks the "Send Message" button, the following happens behind the scenes:
What Happens Step by Step: Form Submission:
Sarah's information (name, email, subject, and message) is sent to the contact view in your Django project. Saving the Submission:
The contact view creates a new instance of the ContactSubmission model using the data provided by Sarah. This instance is saved in the database with the information she submitted. Sending the Email:
Immediately after saving the form data, the view uses Django's send_mail function to send an email from the email in the settings into this email myworkemail@gmail.com with sarah object
Here's my settings related part
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465 # SSL port
EMAIL_USE_SSL = True
EMAIL_HOST_USER = 'myverifiedemail@gmail.com'
EMAIL_HOST_PASSWORD = 'my_app_pass'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Here's my model related part
class ContactSubmission(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
subject = models.CharField(max_length=255)
message = models.TextField()
submitted_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Message from {self.name} - {self.email}"
Here's the view related part
def contact(request):
banner = get_object_or_404(Banner, page='contact')
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
message = request.POST.get('message')
submission = ContactSubmission.objects.create(
name=name,
email=email,
subject=subject,
message=message
)
try:
send_mail(
subject=f"New Contact Submission from {name}",
message=f"Name: {name}\nEmail: {email}\nSubject: {subject}\nMessage: {message}",
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=['myworkemail.com'],
fail_silently=False,
)
messages.success(request, "Your message has been submitted successfully!")
except Exception as e:
messages.error(request, f"An error occurred while sending the email: {str(e)}")
return redirect('contact')
return render(request, 'pages/contact.html', {'banner': banner})
Here's the content block
{% block content %}
<div class="container my-5">
<h2 class="text-center mb-4 text-white">Contact Me</h2>
<form id="contact-form" method="post" action="{% url 'contact' %}">
{% csrf_token %}
<div class="mb-3">
<label for="name" class="form-label text-white">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Your Name" required style="background-color: orange">
</div>
<div class="mb-3">
<label for="email" class="form-label text-white">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Your Email" required style="background-color: orange">
</div>
<div class="mb-3">
<label for="subject" class="form-label text-white">Subject</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required style="background-color: orange">
</div>
<div class="mb-3">
<label for="message" class="form-label text-white">Message</label>
<textarea class="form-control" id="message" name="message" rows="5" placeholder="Your Message" required style="background-color: orange"></textarea>
</div>
<button type="submit" class="hire-me-btn rounded">Send Message</button>
</form>
</div>
{% endblock content %}