Home > Software design >  How to fix an Sending E-mail error in Django
How to fix an Sending E-mail error in Django

Time:05-26

Im getting an error trying to send an e-mail trough Django that I need to solve it I get this error with this settings.py: "getaddrinfo failed"

if DEBUG:
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = '[email protected]'
    EMAIL_HOST_PASSWORD = xxxxx  # important
else:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

and I get this error with this setting.py ConnectionRefusedError at /django-store-account/register/ I need to find a way to fix sending e-mail in Django for me to verify the accounts

if DEBUG:
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = '[email protected]'
    EMAIL_HOST_PASSWORD = xxx  # important
else:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

my urls.py:

path('register/', views.account_register, name='register'),

views.py:

def account_register(request):

    if request.user.is_authenticated:
        return redirect('account:dashboard')

    if request.method == 'POST':
        registerForm = RegistrationForm(request.POST)
        if registerForm.is_valid():
            user = registerForm.save(commit=False)
            user.email = registerForm.cleaned_data['email']
            user.set_password(registerForm.cleaned_data['password'])
            user.is_active = False
            email_to = user.email
            user.save()
            email_subject = 'Ative sua conta'
            current_site = get_current_site(request)
            message = render_to_string('account/registration/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })

            email_body = message

            email = EmailMessage(
                email_subject,
                email_body,
                settings.EMAIL_HOST_USER, [email_to])
            email.send()
            return HttpResponse('registered succesfully and activation sent')
    else:
        registerForm = RegistrationForm()
    return render(request, 'account/registration/register.html', {'form': registerForm})


def account_activate(request, uidb64, token):
    try:
        uid = urlsafe_base64_decode(uidb64)
        user = UserBase.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, user.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        login(request, user)
        return redirect('account:dashboard')
    else:
        return render(request, 'account/registration/activation_invalid.html')

CodePudding user response:

I think EMAIL_HOST is not correct.

EMAIL_HOST = 'smtp.gmail.com'
  • Related