0

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.

Buddy Christ
  • 1,263
  • 7
  • 22

1 Answers1

0

You should not be using user's email address in from field. It should be the address that is in your control. By using user's email in from field, it implies that the email was sent by the user which is not the case, and should never happen.

Moreover, gmail would not allow you to send email from anyone else's email address from your account.

I would suggest you to use your own email address and have user's email address in the message.

RPC
  • 48
  • 4