Django SMTP [Errno 111]连接被拒绝

时间:2017-10-17 16:40:24

标签: python django smtp

我正在尝试使用django和sendgrid SMTP服务器从Web应用程序发送电子邮件。它在本地工作正常,但当我尝试在pythonany的地方使用它时,我得到

error: [Errno 111] Connection refused

相关视图中的代码是

def contact(request):
    if request.method == 'POST':
        form = EmailForm(request.POST)

        if form.is_valid():
            mail = form.cleaned_data

            send_mail(
                'Automated Enquiry',
                'A user is interested in part ' + mail['part'] + '.Their message: ' + mail['msg'],
                mail['email'],
                ['myemail@gmail.com'],
                fail_silently = False,
            )

            return render(request, 'manager/error.html', {'msg': "Thanks, we will be in touch as soon as possible"})

    else:
        part = request.GET.get('part', '')
        form = EmailForm()

    return render(request, 'manager/contact.html', {'form': form, 'part':part})    

在我的setting.py中:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'myuser'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

它也无法在控制台上运行。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

PythonAnywhere上的免费用户限制了对whitelist个站点的Internet访问,并且只允许HTTP / HTTPS协议,因此您无法通过免费帐户与sendgrid建立SMTP连接。

您可以使用免费帐户中的Gmail,而pythonanywhere可以使用instructions here

或者您可以切换到使用sandgrid HTTP API:https://sendgrid.com/docs/Integrate/Frameworks/django.html