我在“关于”页面上设置了一个简单的联系表单,填写字段并单击“提交”后,我得到“成功!”。电子邮件已发送的消息。但是,电子邮件永远不会到达我的收件箱。
我已经在此处阅读过许多有关此问题的帖子(似乎很常见),但我尝试过的所有方法均无效。即使使用django.core.mail.backends.console.EmailBackend
也无法在控制台中打印,但我仍然得到“成功!”提交时出现提示信息。
我的settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.getenv("SECRET_KEY")
DEBUG = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.live.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxx@hotmail.com'
EMAIL_HOST_PASSWORD = os.environ['EMAIL_PASSWORD']
...
我的views.py:
...
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse
from django.shortcuts import redirect
from .forms import ContactForm
...
class AboutPage(TemplateView):
template_name = 'database/templates/about.html'
def about(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, ['xxxx@hotmail.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, 'about.html', {'form': form})
def success(request):
return render(request, 'success.html')
我的urls.py:
from django.urls import path
from . import views
urlpatterns = [
...
path('about/', views.about, name='about'),
path('success/', views.success, name='success'),
...
]
我的about.html模板:
{% extends "base.html" %}
{% block content %}
...
<h3>Contact</h3>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="form-actions">
<button type="submit">Send</button>
</div>
</form>
...
{% endblock %}
还有我的success.html模板:
{% extends "base.html" %}
{% block content %}
...
<h1>Success!</h1>
<p>Thank you for your message!</p>
...
{% endblock %}
我想念什么?任何建议将不胜感激。
编辑
根据@YevheniiM的建议,在运行django-shell之后,这就是我得到的内容(在评论中发布的时间太长了):
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>> send_mail('subject', 'message', 'example@example.org', ['test@example.org'])
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: example@example.org
To: test@example.org
Date: Sun, 13 Oct 2019 23:33:35 -0000
Message-ID: <157100961593.25849.16400852689643226474@localhost>
message
-------------------------------------------------------------------------------
1
>>>
编辑2
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>> send_mail('subject', 'message', 'example@example.org', ['test@example.org'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/meganmccarty/.virtualenvs/django3/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/meganmccarty/.virtualenvs/django3/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/meganmccarty/.virtualenvs/django3/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/meganmccarty/.virtualenvs/django3/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 70, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python3.7/smtplib.py", line 730, in login
raise last_exception
File "/usr/lib/python3.7/smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.7/smtplib.py", line 642, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [MN2PR01CA0032.prod.exchangelabs.com]')
>>>
编辑3 对于任何阅读此书的人来说,事实证明pythonanywhere和hotmail(我在这里都试图使用这两者)不能一起工作。感谢您解决此问题,@ YevheniiM!切换为使用Gmail帐户后,一切运行正常!