我想将联系表单发送到我的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
问题出在哪里? 有关更多解释,请使用:
答案 0 :(得分:1)
您应该在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'