繁体   English   中英

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

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

视图.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
        )

设置.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

联系方式。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>

我已经为联系我页面创建了这个代码。 现在,当用户提交包含message-name、message-email 和 message字段的联系表时,我收到 email ,其中FROMTO都是我的email-id

它没有检索用户 email。 但是字段的 rest 工作正常。

甚至使用DebuggingServer进行了测试,并且可以按预期工作。 好像我在 setting.py 中遗漏了一些东西,因为那是我更改的一个文件。 我不明白我哪里错了。 任何帮助表示赞赏。

您不应该在 from 字段中使用用户的 email 地址。 它应该是您可以控制的地址。 通过在 from 字段中使用用户的 email,这意味着 email 是由用户发送的,但事实并非如此,也不应该发生。

此外,gmail 将不允许您从其他人的 email 地址从您的帐户发送 email。

我建议您使用自己的 email 地址并在消息中包含用户的 email 地址。

我有同样的问题,你不能在没有密码的情况下发送 email。 联系页面使用您的 email 将消息发送给自己。 在正文中显示客户端信息。 请参阅下面的链接以获取出路... Django-python email 表单未通过 gmail935BBE4C7CAAFF83 转发发件人的 email 地址

暂无
暂无

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

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