Mail With Python
Mail With Python
Home Django Python C# ADO.NET Java PHP HTML CSS JavaScript jQuery XML XSLT
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 1/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
In this tutorial, we will learn how to send the confirmation mail using the Django when someone registers on our
web app. We will discuss an easy way to build this. But Django provides many other options like django allauth
application, django-registration, django-registration-redux. Those applications make this task very straightforward.
Before starting, make sure that you must knowledge of Django and Python programming language. Create the
basic setup of Django and configure the settings.
Configure Settings
First we need to configure the email host server in the settings.py for confirmation mail. Add the below
configuration in the settings.py file.
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
We used the email-id along with the password and gmail SMTP host server. You can use the other SMTP server as
well.
Generate Token
We need to create the token that will be used in email confirmation URL. Create the token.py file in the token and
add the below code.
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 2/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
We used the PasswordTokenGenerator class that is used to reset the password. In the above code, we generated
the unique token for confirmation.
forms.py
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
In the above code, we imported the UserCreationForm and built-in User. Then we created a SignupForm including
extra field email in SignupForm.
view.py
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
# save form in the memory not in database
user = form.save(commit=False)
user.is_active = False
user.save()
# to get the domain of the current site
current_site = get_current_site(request)
mail_subject = 'Activation link has been sent to your email id'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
Here we create a view of sign up, it got information using POST method and valid it. We have used the commit =
False because it allows us to get the model object and add some extra attribute. Here we have done user.is_active
= False which means user cannot login until email is verified.
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 4/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
Then we used the EmailMessage() function to send mail along with the subject, message. Email message create by
a template.
templates/acc_active_email.html
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
This template create an email body with activate link that will send for application.
香港服务器,CN2直连,低
至880元
快速稳定安全,免备案,99%的在线率保障!
恒创科技 打开
views.py
⇧ SCROLL TO TOP
def activate(request, uidb64, token):
https://www.javatpoint.com/django-user-registration-with-email-confirmation 5/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
User = get_user_model()
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.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()
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
We have added the activate function after the signup function. This view will check token it valid then user will
activate and login. We set user.is_active = True which means user can login.
URLS
{% extends 'base.html' %}
{% block content %}
<div class = "container">
<h2>Sign up</h2>
⇧ SCROLL TO TOP
<form method="post">
https://www.javatpoint.com/django-user-registration-with-email-confirmation 6/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none ">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
</div>
{% endblock %}
The form will look like as below. When the user click on the submit button the activation link send their entered
email id.
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 7/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
When you click on the sign up button, the confirmation email send on given email-id.
Click on the received link and now you are ready to login.
Note -Make sure that the less secure app access setting should be turned on. Otherwise, you will face smtp error.
Tada, we have successfully created an email configuration based user registration. This process is straightforward
and required less code to complete. You can make it more improvement by adding more functionality such as
attractive template, redirect to login, etc.
← Prev Next →
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 8/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
Feedback
Your IT career in
Open
Germany
Make it in Germany
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 9/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
React Native Python Design Python Pillow Python Turtle Keras tutorial
tutorial Patterns tutorial tutorial
Keras
React Native Python Design Python Pillow Python Turtle
Patterns
Preparation
Trending Technologies
Machine DevOps
Learning Tutorial Tutorial
Machine Learning DevOps
⇧ SCROLL TO TOP
B.Tech / MCA
https://www.javatpoint.com/django-user-registration-with-email-confirmation 10/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 11/12
4/11/22, 3:36 PM Django User Registration with Email Confirmation - javatpoint
⇧ SCROLL TO TOP
https://www.javatpoint.com/django-user-registration-with-email-confirmation 12/12