在django-heroku上使用mailgun发送电子邮件时出现错误110



我的设置.py:

EMAIL_HOST = "smtp.mailgun.org"
MAIL_PORT = 587
EMAIL_HOST_USER = os.environ["MAILGUN_SMTP_LOGIN"]
EMAIL_HOST_PASSWORD = os.environ["MAILGUN_SMTP_PASSWORD"]
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

我的观点.py:

import re
import os
from django.http import HttpResponse
from django.template import loader
from django.contrib.auth.hashers import make_password
import time
import secrets
from .models import User
from .models import EmailConfirmation
from django.core.mail import send_mail
from threading import Thread
import threading
noReply = re.sub(".*@", "noreply@", os.environ["MAILGUN_SMTP_LOGIN"])
def threadingSendMail(subject, message, from_email, recipientList):
send_mail(
subject=subject,
message=message,
from_email=from_email,
recipient_list=recipientList
)
def wait(request):
if request.method == "POST":
password = request.POST["password"]
email = request.POST["email"]
if not re.match(r"[^@]+@[^@]+.[^@]+", email):
return HttpResponse("Email not valid")
else:
if User.objects.filter(email=email).exists():
return HttpResponse("Email already exists")
else:
theUser = User.objects.create(email=email, password=make_password(password), confirmed=False)
hashKey = secrets.token_hex(16)
EmailConfirmation.objects.create(email=theUser,emailHash=hashKey) 
verificationLink = request.get_host() + "/" + str(hashKey)

threading.Thread(target=threadingSendMail, args=("Email conf",
verificationLink,noReply,[email],)).start()
return HttpResponse("pass")
else:
return HttpResponse("hello")

我在heroku日志中的错误:

2022-08-27T10:55:15.473726+00:00 app[web.1]: Exception in thread Thread-1 (threadingSendMail):
2022-08-27T10:55:15.473737+00:00 app[web.1]: Traceback (most recent call last):
2022-08-27T10:55:15.473738+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
2022-08-27T10:55:15.473739+00:00 app[web.1]: self.run()
2022-08-27T10:55:15.473739+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/threading.py", line 953, in run
2022-08-27T10:55:15.473810+00:00 app[web.1]: self._target(*self._args, **self._kwargs)
2022-08-27T10:55:15.473820+00:00 app[web.1]: File "/app/djangocode/stocks/views.py", line 21, in threadingSendMail
2022-08-27T10:55:15.473912+00:00 app[web.1]: send_mail(
2022-08-27T10:55:15.473921+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/__init__.py", line 87, in send_mail
2022-08-27T10:55:15.474010+00:00 app[web.1]: return mail.send()
2022-08-27T10:55:15.474018+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/message.py", line 298, in send
2022-08-27T10:55:15.474128+00:00 app[web.1]: return self.get_connection(fail_silently).send_messages([self])
2022-08-27T10:55:15.474146+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
2022-08-27T10:55:15.474235+00:00 app[web.1]: new_conn_created = self.open()
2022-08-27T10:55:15.474243+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 80, in open
2022-08-27T10:55:15.474326+00:00 app[web.1]: self.connection = self.connection_class(
2022-08-27T10:55:15.474337+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/smtplib.py", line 255, in __init__
2022-08-27T10:55:15.474458+00:00 app[web.1]: (code, msg) = self.connect(host, port)
2022-08-27T10:55:15.474467+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/smtplib.py", line 341, in connect
2022-08-27T10:55:15.474589+00:00 app[web.1]: self.sock = self._get_socket(host, port, self.timeout)
2022-08-27T10:55:15.474590+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/smtplib.py", line 312, in _get_socket
2022-08-27T10:55:15.474707+00:00 app[web.1]: return socket.create_connection((host, port), timeout,
2022-08-27T10:55:15.474716+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/socket.py", line 845, in create_connection
2022-08-27T10:55:15.474920+00:00 app[web.1]: raise err
2022-08-27T10:55:15.474928+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/socket.py", line 833, in create_connection
2022-08-27T10:55:15.475117+00:00 app[web.1]: sock.connect(sa)
2022-08-27T10:55:15.475157+00:00 app[web.1]: TimeoutError: [Errno 110] Connection timed out

正如标题所说,我正在使用heroku、django和mailgun。

有人有什么想法吗?最奇怪的是,这确实在本地工作,但在Heroku服务器上不工作。通常在提交表单后,大约需要一分钟左右的时间才会弹出此消息。我使用线程,这样当页面加载时,电子邮件就可以在后台发送,页面确实按预期加载,pass出现了,只是这个错误。

在您的设置中。请使用EMAIL_PORT=587而不是MAIL_PORT=587

Django smtp正在使用设置。电子邮件端口

这是django/core/mail/backends/smtp.py源代码的一部分,其中端口值被分配

class EmailBackend(BaseEmailBackend):
"""
A wrapper that manages the SMTP network connection.
"""
def __init__(
self,
host=None,
port=None,
username=None,
password=None,
use_tls=None,
fail_silently=False,
use_ssl=None,
timeout=None,
ssl_keyfile=None,
ssl_certfile=None,
**kwargs,
):
super().__init__(fail_silently=fail_silently)
self.host = host or settings.EMAIL_HOST
self.port = port or settings.EMAIL_PORT

最新更新