dwmyfriend
dwmyfriend

Reputation: 224

Django SMTP [Errno 111] Connection refused

I am trying to send email from a web app using django and a sendgrid SMTP server. It works fine locally but when I try to use it on pythonanywhere I get

error: [Errno 111] Connection refused

The code from the relevant view is

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'],
                ['[email protected]'],
                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})    

And in my setting.py:

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

It didn't work from the console either. Any help would be greatly appreciated.

Upvotes: 2

Views: 2142

Answers (1)

hwjp
hwjp

Reputation: 16091

free users on PythonAnywhere have restricted Internet access to a whitelist of sites, and only the HTTP/HTTPS protocols are allowed, so you cannot make SMTP connections to sendgrid from a free account.

you can use gmail from a free account, and pythonanywhere have instructions here.

or you can switch to using the sandgrid HTTP api: https://sendgrid.com/docs/Integrate/Frameworks/django.html

Upvotes: 1

Related Questions