This code has worked me before but its not working anymore.everything is running fine but its not sending email to the users after signup process done. Is there any solutions for this code?
here is views.py
def activate(req, uidb64, token):
try:
uid = urlsafe_base64_decode(uidb64).decode()
user = User.objects.get(id=uid)
except(TypeError, ValueError):
user = None
if user and activation_token.check_token(user, token):
user.is_active = True
user.save()
messages.info(req, 'Your Account activated. Now Login')
return redirect("shop:users_signin")
else:
messages.error(req, "Activation link is Invalid.")
def users_signup(req):
if req.method == "POST":
form = UserSignupForm(req.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
site = get_current_site(req)
mail_subject = "Confirmation message"
message = render_to_string('shop/activate_mail.html', {
"user": user,
'domain': site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': activation_token.make_token(user)
})
to_email = form.cleaned_data.get('email')
to_list = [to_email]
from_email = settings.EMAIL_HOST_USER
send_mail(mail_subject, message, from_email, to_list, fail_silently=True)
messages.success(req,"Thanks for your registration. A confirmation link has been sent to your email")
else:
form = UserSignupForm()
return render(req,'shop/users_signup.html',{'form':form})
here is my url
urls.py
path('signup/user/',views.users_signup,name='users_signup'),
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
here is my settings.py
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER ='my email'
EMAIL_HOST_PASSWORD = 'my pass'
EMAIL_POST = '587'
here is my tokens.py
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class tokenGenerate(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.id)+six.text_type(timestamp)+six.text_type(user.is_active)
)
activation_token=tokenGenerate()
here is my activate_mail.html
activate_mail.html
Hi {{user.username}} Thanks for your registration in our college finder application.
Please click on the link below to activate your account.
http://{{domain}}{% url 'shop:activate' uidb64=uid token=token %}