我尝试使用send_mail()函数在Django中建立联系人电子邮件表单。它在测试环境中可以正常工作,但在生产环境中则不能。提交表单后,浏览器中出现服务器错误(500)。我收到带有以下内容的django错误报告电子邮件:
异常类型:SMTPDataError 异常值:(550,b'不正确的发件人标头(LOCAL)')
在设置中,我使用相同的电子邮件地址进行错误报告和电子邮件表格,因此它在一种情况下有效,而在另一种情况下则无效。上述错误是什么意思?知道如何解决吗?
kontakt.html
<form method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.from_email.errors }}
<label for="{{ form.from_email.id_for_label }}">Email:</label><br/>
{{ form.from_email }}
</div>
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="{{ form.subject.id_for_label }}">Subject:</label><br/>
{{ form.subject }}
</div>
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="{{ form.message.id_for_label }}">Message:</label><br/>
{{ form.message }}
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
views.py
from django.shortcuts import render, redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from userform.forms import ContactForm
def kontakt(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, ['admin@example.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('thanks')
return render(request, "kontakt.html", {'form': form})
def thanks(request):
return HttpResponse('Message sent')
forms.py
from django import forms
class ContactForm(forms.Form):
from_email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea)
urls.py
from django.conf.urls import url
from userform import views
urlpatterns = [
...
url(r'^kontakt/', views.kontakt, name='kontakt'),
url(r'^thanks/$', views.thanks, name='thanks'),
]
settings.py
EMAIL_HOST = 'smtp.example.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'test@example.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SERVER_EMAIL = 'test@example.com'
ADMINS=[('admin','admin@example.com')]
追踪
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/pdudko/apps/userform/views.py" in kontakt
88. send_mail(subject, message, from_email, ['admin@example.com'])
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/mail/__init__.py" in send_mail
60. return mail.send()
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/mail/message.py" in send
294. return self.get_connection(fail_silently).send_messages([self])
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in send_messages
110. sent = self._send(message)
File "/home/pdudko/venv/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in _send
126. self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python3.4/smtplib.py" in sendmail
800. raise SMTPDataError(code, resp)
Exception Type: SMTPDataError at /kontakt/
Exception Value: (550, b'Incorrect sender header (LOCAL)')