Django -邮件不发送,端口更改为25,但在seetings.py中设置为587

e4yzc0pl  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(89)

未发送电子邮件。有趣的是...之前(像3个月前)整个代码工作得很好。

设置:

DEBUG = True

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT: 587
EMAIL_HOST_USER = 'xyz@gmail.com'
EMAIL_HOST_PASSWORD = 'xyz'
EMAIL_USE_TLS = True

ALLOWED_HOSTS = []

字符串

view.py:

def index(request):
    """The home page."""

    # Send a message.
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form_email = EmailForm()
        form_message = EmailMessageForm()
    else:
        # POST data submitted; proecess data.
        form_email = EmailForm(data=request.POST)
        form_message = EmailMessageForm(data=request.POST)
        if form_email.is_valid() and form_message.is_valid():
            try:
                email = Email.objects.get(text=request.POST['text'])
            except:
                form_email.save()
                email = Email.objects.last()
            
            message_db = form_message.save(commit=False)
            message_db.email = email
            message_db.save()

            message_owner = (
                f'New email on your website from {request.POST["text"]}',
                f"Email has been sent from: {request.POST['text']}\n\n"
                f"Full message:\n\"{request.POST['message']}\"",
                'settings.EMAIL_HOST_USER',
                ['my@gmail.com',],
            )

            message_guest = ('Your email has been sent',
                "Many thanks for getting in contact with me. Your message was "
                "successfully sent. I will reach out to you as soon as possible."
                f"\n\nYour message:\n\"{request.POST['message']}\"",
                'settings.EMAIL_HOST_USER',
                [request.POST['text'],],
            )

            send_mass_mail((message_owner, message_guest), fail_silently=False)

            return redirect('home:contact_thank_you')

    # Display a blank or invalid form.
    context = {'form_email': form_email, 'form_message': form_message}
    return render(request, 'home/index.html', context)

**回溯:**请求方式:后

请求URL:http://127.0.0.1:8000/
Django版本:4.1.5
例外类型:TimeoutError
异常值:[WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应
饲养时间:home.views.index
x1c 0d1x的数据

**我找到了原因,但我找不到任何解决方案来解决它!**这是我收到的电子邮件时收到的:



seetings.py中端口被设置为587(如上所示),但不知何故,在发送电子邮件后,这个值是25而不是587
在文档中你可以看到25是默认值,但是为什么我的设置没有覆盖它呢?

wfveoks0

wfveoks01#

你肯定不想在电子邮件地址周围加上引号。还请注意,与元组不同,创建一个一项列表时不需要结尾逗号:

message_owner = (
                f'New email on your website from {request.POST["text"]}',
                f"Email has been sent from: {request.POST['text']}\n\n"
                f"Full message:\n\"{request.POST['message']}\"",
                settings.EMAIL_HOST_USER,
                ['my@gmail.com'],
            )

            message_guest = ('Your email has been sent',
                "Many thanks for getting in contact with me. Your message was "
                "successfully sent. I will reach out to you as soon as possible."
                f"\n\nYour message:\n\"{request.POST['message']}\"",
                settings.EMAIL_HOST_USER,
                [request.POST['text']],
            )

字符串

ocebsuys

ocebsuys2#

我发现不是最好的解决方案,但它工作...
如果您有同样的问题,请访问:
\AppData\Local\Programs\Python\Python311\Lib\smtplib.py
然后打开这个文件并将port替换为587:100d1x

的字符串
但这不是最好的做法,我希望有人找到更好的解决方案如何修复它。

相关问题