【发布时间】:2015-10-18 06:00:23
【问题描述】:
当我尝试通过 ./manage.py shell 发送时,发送一封电子邮件需要几分钟时间。当我在浏览器中提交表单后尝试发送用户验证电子邮件时,浏览器超时并显示 504,但最终发送了电子邮件。会发生什么?
settings.py
...
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = os.environ.get('PASSWORD')
...
views.py
class SignUpView(CreateView):
model = User
template_name = 'eventMap/register.html'
form_class = RegistrationForm
success_url="/"
def form_valid(self, form):
form.save()
username = form.cleaned_data['username']
email = form.cleaned_data['email']
salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
activation_key = hashlib.sha1(salt+email).hexdigest()
key_expires = datetime.datetime.today() + datetime.timedelta(2)
#Get user by username
user=User.objects.get(username=username)
# Create and save user profile
new_profile = UserProfile(user=user, activation_key=activation_key,
key_expires=key_expires)
new_profile.save()
# Send email with activation key
email_subject = 'Account confirmation'
email_body = "Hey %s, thanks for signing up. To activate your account, click this link within \
48hours http://mywebsite.com/accounts/confirm/%s" % (username, activation_key)
send_mail(email_subject, email_body, 'myemail@gmail.com',
['mytestemail@gmail.com'], fail_silently=False)
return super(SignUpView, self).form_valid(form)
我在这篇文章中看到了类似的内容,但日志中没有提到任何关于不合格主机名等的内容 /var/log/mail.log
Jul 27 16:26:04 django postfix/qmgr[5975]: CAF7C1226F2: from=<>, size=3063, nrcpt=1 (queue active)
Jul 27 16:26:34 django postfix/smtp[12874]: connect to example.com[2606:2800:220:1:248:1893:25c8:1946]:25: Connection timed out
Jul 27 16:27:04 django postfix/smtp[12874]: connect to example.com[93.184.216.34]:25: Connection timed out
Jul 27 16:27:04 django postfix/smtp[12874]: CAF7C1226F2: to=<myemail@example.com>, relay=none, delay=368178, delays=368118/0.02/60/0, dsn=4.4.1, status=deferred (connect to example.com[93.184.216.34]:25: Connection timed out)
【问题讨论】:
-
我也遇到过这个问题。使用来自 django 1.6 的代码库,电子邮件会在几秒钟内发送,django 1.8 上的相同代码库需要 1-5 分钟。我决定只打开一个新线程来解决这个问题。这是我正在使用的代码github.com/ui/django_asynchronous_send_mail
标签: python django email smtp gmail