簡體   English   中英

在 django 上用 mailgun 發送 email 時出現錯誤 110 heroku

[英]Error 110 sending email with mailgun on django heroku

我的設置.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 服務器上無效。 通常在提交表單后,大約需要一分鍾左右的時間才會彈出此消息。 我使用線程,以便 email 可以在頁面加載時在后台發送,頁面確實按預期加載,pass 出現,只是這個錯誤。

在您的 settings.py 中使用 EMAIL_PORT=587 而不是 MAIL_PORT=587

Django smtp 正在使用 settings.EMAIL_PORT

這是分配端口值的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM