Chapter 5. User Management

Django Allauth (4) – Email Verification via Gmail

Django Allauth (4) – Email Verification via Gmail
Tag:

This lesson will explain how to set up email verification via Gmail. The approach can change depending on Google's security policy. This approach is valid as of April 2023.

1. Set up a Gmail App password

You need to set up a Gmail App password to send email verification emails via Gmail. Go to your Gmail account in your browser and click on "Manage your Google Account".

Django-Allauth-4---Email-Verification-via-Gmail

Go to 2-Step Verification under the Security section.

Django-Allauth-4---Email-Verification-via-Gmail

If you haven't set it up yet, follow the guidance to set up 2-Step Verification and turn it on.

Django-Allauth-4---Email-Verification-via-Gmail

Go to the App passwords section at the bottom.

Django-Allauth-4---Email-Verification-via-Gmail

Generate an app password with a custom name.

Django-Allauth-4---Email-Verification-via-Gmail

You'll see that an app password was generated. Copy the password as we will use it in settings.py.

Django-Allauth-4---Email-Verification-via-Gmail

2. Edit settings.py

To change the console-based email verification setting to email-based verification, edit settings.py.

  • Change EMAIL_BACKEND to 'django.core.mail.backends.smtp.EmailBackend'
  • Add email settings

The EMAIL_BACKEND setting enables Django to send emails using the SMTP protocol.

config/settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_account@gmail.com'
EMAIL_HOST_PASSWORD = 'App Password'
EMAIL_USE_SSL = True
EMAIL_USE_TLS = False
EMAIL_PORT = 465
DEFAULT_FROM_EMAIL = 'your_account@gmail.com'

Make sure that only one of TLS or SSL can be True, and that the port setting is aligned with the protocol. In this case, we use EMAIL_USE_SSL = True, EMAIL_USE_TLS = False, and EMAIL_PORT = 465.

3. Check the results

To check the results, try to sign up again. This time, you need to use an actual email address.

Django-Allauth-4---Email-Verification-via-Gmail

Once you click on the Sign Up button, you'll see a message saying that a verification email has been sent out.

Django-Allauth-4---Email-Verification-via-Gmail

Check your email. You'll see the verification email in your inbox.

Django-Allauth-4---Email-Verification-via-Gmail

Click the link, and then you'll see a confirmation message. After you press the Confirm button, your account will be created...

Django-Allauth-4---Email-Verification-via-Gmail

...and you'll be ready to sign in (log in) to the service.

Django-Allauth-4---Email-Verification-via-Gmail

Sign in with your registered email and password. You'll jump to the home page (You are successfully logged in to the app).

Django-Allauth-4---Email-Verification-via-Gmail

IdeaNote: Delete Users

When you are working on the email setting, you may want to test functionalities multiple times. However, the app blocks new registrations that use the same email address. Delete the user from the Django admin to test it again with the same email address.

Go to the Users section on the left menu. Select the user to delete, select "Delete selected users" in the dropdown, and press the Go button.

Django-Allauth-4---Email-Verification-via-Gmail

Make sure that you delete the user, not just their email address; or else, even though you delete the email address, the user won't be deleted. But once you delete the user, the registered email address will also bedeleted.

This lesson will explain how to set up email verification via Gmail. The approach can change depending on Google's security policy. This approach is valid as of April 2023.

1. Set up a Gmail App password

You need to set up a Gmail App password to send email verification emails via Gmail. Go to your Gmail account in your browser and click on "Manage your Google Account".

Django-Allauth-4---Email-Verification-via-Gmail

Go to 2-Step Verification under the Security section.

Django-Allauth-4---Email-Verification-via-Gmail

If you haven't set it up yet, follow the guidance to set up 2-Step Verification and turn it on.

Django-Allauth-4---Email-Verification-via-Gmail

Go to the App passwords section at the bottom.

Django-Allauth-4---Email-Verification-via-Gmail

Generate an app password with a custom name.

Django-Allauth-4---Email-Verification-via-Gmail

You'll see that an app password was generated. Copy the password as we will use it in settings.py.

Django-Allauth-4---Email-Verification-via-Gmail

2. Edit settings.py

To change the console-based email verification setting to email-based verification, edit settings.py.

  • Change EMAIL_BACKEND to 'django.core.mail.backends.smtp.EmailBackend'
  • Add email settings

The EMAIL_BACKEND setting enables Django to send emails using the SMTP protocol.

config/settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_account@gmail.com'
EMAIL_HOST_PASSWORD = 'App Password'
EMAIL_USE_SSL = True
EMAIL_USE_TLS = False
EMAIL_PORT = 465
DEFAULT_FROM_EMAIL = 'your_account@gmail.com'

Make sure that only one of TLS or SSL can be True, and that the port setting is aligned with the protocol. In this case, we use EMAIL_USE_SSL = True, EMAIL_USE_TLS = False, and EMAIL_PORT = 465.

3. Check the results

To check the results, try to sign up again. This time, you need to use an actual email address.

Django-Allauth-4---Email-Verification-via-Gmail

Once you click on the Sign Up button, you'll see a message saying that a verification email has been sent out.

Django-Allauth-4---Email-Verification-via-Gmail

Check your email. You'll see the verification email in your inbox.

Django-Allauth-4---Email-Verification-via-Gmail

Click the link, and then you'll see a confirmation message. After you press the Confirm button, your account will be created...

Django-Allauth-4---Email-Verification-via-Gmail

...and you'll be ready to sign in (log in) to the service.

Django-Allauth-4---Email-Verification-via-Gmail

Sign in with your registered email and password. You'll jump to the home page (You are successfully logged in to the app).

Django-Allauth-4---Email-Verification-via-Gmail

IdeaNote: Delete Users

When you are working on the email setting, you may want to test functionalities multiple times. However, the app blocks new registrations that use the same email address. Delete the user from the Django admin to test it again with the same email address.

Go to the Users section on the left menu. Select the user to delete, select "Delete selected users" in the dropdown, and press the Go button.

Django-Allauth-4---Email-Verification-via-Gmail

Make sure that you delete the user, not just their email address; or else, even though you delete the email address, the user won't be deleted. But once you delete the user, the registered email address will also bedeleted.

Tag: