Views.py
def contact(request):
if request.method == 'POST':
message_name = request.POST['message-name']
message_email = request.POST['message-email']
message = request.POST['message']
# send an email
send_mail(
'Message from ' + message_name, # subject
message, # message
message_email, # from email
['myEmailId@gmail.com'], # to email
)
settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myEmailId@gmail.com'
EMAIL_HOST_PASSWORD = '<myaccount app password>'
EMAIL_USE_TLS = True
contact.html
<form action="{% url 'contact' %}" method="POST" class="bg-light p-5 contact-form">
{% csrf_token %}
<div class="form-group">
<input type="text" name="message-name" class="form-control" placeholder="Your Name">
</div>
<div class="form-group">
<input type="email" name="message-email" class="form-control" placeholder="Your Email">
</div>
<div class="form-group">
<textarea name="message" id="" cols="30" rows="7" class="form-control" placeholder="Message">
</textarea>
</div>
<div class="form-group">
<input type="submit" value="Send Message" class="btn btn-primary py-3 px-5">
</div>
</form>
I have created this code for the contact-me page. Now when user Submits the Contact Form which has the fields message-name, message-email and message, I receive the email where FROM and TO are both my email-id.
It is not retrieving the users email. But rest of the fields are working fine.
Even Tested with DebuggingServer and that works as expected. Seems like I am missing something in the setting.py because that is the one file I have changed. I don't understand where I am going wrong. Any help appreciated.