gpt4 book ai didi

python - django将联系表格发送到电子邮件引发错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:10 25 4
gpt4 key购买 nike

我想将联系表发送到我的 Gmail。

views.py:

def contact(request):
errors = []
if request.method == 'POST':
if not request.POST.get('subject', ''):
errors.append('Enter a subject.')
if not request.POST.get('message', ''):
errors.append('Enter a message.')
if request.POST.get('email') and '@' not in request.POST['email']:
errors.append('Enter a valid e-mail address.')
if not errors:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('email', 'noreply@example.com'),
['mypersonalemail@gmail.com'],
)
return HttpResponseRedirect('/contact/thanks/')
return render(request, 'contact_form.html',
{'errors': errors})

contact_form.html:

</head>
<body>
<div>
<h1>Contact us</h1>

{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}

<form action="/contact/" method="post">{% csrf_token %}{% csrf_token %}
<p>Subject: <input type="text" name="subject"></p>
<p>Your e-mail (optional): <input type="text" name="email"></p>
<p>Message: <textarea name="message" rows="10" cols="50"> </textarea></p>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>

但是当我提交表单时它会引发此错误:

Exception Type:     ConnectionRefusedError
Exception Value: [Errno 111] Connection refused
Exception Location: /usr/lib/python3.4/socket.py in create_connection, line 503

问题出在哪里?为了获得更多解释,我使用:

  • Django 版本:1.8.2
  • Python版本:3.4.3

最佳答案

您应该在 settings.py 文件中为 gmail 设置正确的连接设置:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your gmail account'
EMAIL_HOST_PASSWORD = 'your gmail account password'
DEFAULT_FROM_EMAIL = 'your gmail account'
DEFAULT_TO_EMAIL = 'to email'

关于python - django将联系表格发送到电子邮件引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com