Django getting 530, 5.7.0 Authentication Required despite using google's App Paswords

Have 2fa on Google, created the password, putting in the correct email and app password in the settings.py, yet still get Authentication Error. Tried both 587(TLS=True) and 465(SSL=True) but didn't seem to change anything.

settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
EMAIL_HOST_USER = 'mygmail@gmail.com'

EMAIL_PASSWORD = "my16digitpassword"
DEFAULT_FROM_EMAIL = 'mygmail@gmail.com'

What might be the problem/solution? Any answers for this problem feature "use google app password".

from django.shortcuts import render, get_object_or_404, redirect
from .models import Post
from .forms import PostForm


def Home(request, id=None):
    if id:
        post = get_object_or_404(Post, id=id)
    else:
        post = None
    
    if request.method == "POST":
        if "delete" in request.POST:
            post = get_object_or_404(Post, id= request.POST.get("delete"))
            post.delete()
            return redirect("home")
        
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            return redirect("home")
        
    else:
        form = PostForm(instance=post)

    posts = Post.objects.all()
    return render(request, "blog/home.html", {"form": form, "posts": posts})

You need a app password for it.

If you have 2-Step Verification enabled for your Google Account:

  1. go to your Google Account security settings

  2. scroll to the "2-Step Verification" section

  3. click on the "App passwords" option

  4. provide a name for the app and then click "Create" to generate a 16-digit password

  5. copy it immediately (it will not be displayed again)

  6. use that password in EMAIL_HOST_PASSWORD

You can test sending emails with gmail outside of django with this script:

import smtplib
from email.mime.text import MIMEText

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = "mygmail@gmail.com"
EMAIL_HOST_PASSWORD = "my16digitpassword"
DEFAULT_FROM_EMAIL = "mygmail@gmail.com"

server = smtplib.SMTP_SSL(EMAIL_HOST, EMAIL_PORT)
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)

msg = MIMEText("TEST")
msg['Subject'] = "TEST"
msg['From'] = DEFAULT_FROM_EMAIL
server.sendmail(DEFAULT_FROM_EMAIL, "test@example.com", msg.as_string())

Change EMAIL_PASSWORDEMAIL_HOST_PASSWORD

Use the correct port/protocol combo

Verify the app password belongs to the same account

Back to Top