简体   繁体   English

To 和 From 在 django send_mail() 中返回相同的 email id

[英]To and From return the same email id in django send_mail()

Views.py视图.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设置.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联系方式。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 .现在,当用户提交包含message-name、message-email 和 message字段的联系表时,我收到 email ,其中FROMTO都是我的email-id

It is not retrieving the users email.它没有检索用户 email。 But rest of the fields are working fine.但是字段的 rest 工作正常。

Even Tested with DebuggingServer and that works as expected.甚至使用DebuggingServer进行了测试,并且可以按预期工作。 Seems like I am missing something in the setting.py because that is the one file I have changed.好像我在 setting.py 中遗漏了一些东西,因为那是我更改的一个文件。 I don't understand where I am going wrong.我不明白我哪里错了。 Any help appreciated.任何帮助表示赞赏。

You should not be using user's email address in from field.您不应该在 from 字段中使用用户的 email 地址。 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.通过在 from 字段中使用用户的 email,这意味着 email 是由用户发送的,但事实并非如此,也不应该发生。

Moreover, gmail would not allow you to send email from anyone else's email address from your account.此外,gmail 将不允许您从其他人的 email 地址从您的帐户发送 email。

I would suggest you to use your own email address and have user's email address in the message.我建议您使用自己的 email 地址并在消息中包含用户的 email 地址。

I had the same problem, You can't send email without password.我有同样的问题,你不能在没有密码的情况下发送 email。 the contact page uses your email to send the message to itself.联系页面使用您的 email 将消息发送给自己。 Display the client information in the body.在正文中显示客户端信息。 See the link below for a way out... Django-python email form is not forwarding sender's email address via gmail smtp请参阅下面的链接以获取出路... Django-python email 表单未通过 gmail935BBE4C7CAAFF83 转发发件人的 email 地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM