Cannot Send email

I was facing a problem from the email functionality of django. Here is a piece of code dealing of registering new user’s account and sending mail to them. Instead of sending mails the account is automatically got created !! Is there anybody other than me facing same problem on the email functionality of django?.. I have done all stuffs like setting less_secure_app=on and etc, but!! nothing is happenning
Can I get a help?

class UserSerializer(serializers.ModelSerializer):
    confirm_password = serializers.CharField(read_only=True)
    class Meta:
        model=User
        fields=('username','email','password','confirm_password','first_name','last_name')
        validators = [
            UniqueTogetherValidator(
                queryset=User.objects.all(),
                fields=['username','email','password']
            )
        ]


    def register(self, validated_data):
        if validated_data['password'] != validated_data['confirm_password']:
            raise serializers.ValidationError({"Message":"Passwords Do not Match!!"})
        else:
            try:
                user_obj=User.objects.create_user(
                    username=validated_data['username'],
                    email=validated_data['email'],
                    password=validated_data['password']
                )
                user_obj.first_name=validated_data['first_name'],
                user_obj.last_name=validated_data['last_name']
                user_obj.save(force_insert=True)
                if self.log_user_in(validated_data=validated_data) == 1:
                    subject="Authetication for the course mail"
                    message="If you receive this mail, that means you have succesfully autheticated and logged in in the account also"
                    user_obj.email_user(subject=subject,message=message,from_email=EMAIL_HOST_USER)
                    return user_obj
            except Exception as error:
                raise serializers.ValidationError({"Message":error})

    def log_user_in(self,validated_data):
        user_obj=authenticate(username=validated_data['username'],password=validated_data['password'])
        if user_obj:
            login(user=user_obj)
            return 1
        else:
            return 0

I have attached the piece of code here , to check is there any fault within it!!

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post, please remember to do this in the future.)

We’ll need to see your EMAIL related settings from your settings.py file. (Please remember to remove your email password.)

# Email Attributes
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = 'xxxxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxx'

I’m not seeing anything wrong here, so if I had to debug this, I’d be adding print statements in the register function to see what the important values are at each step of the way. (This would include adding print statements in log_user_in as well to see what’s happening there.)

1 Like

okk I will try and let you know if I find any hidden error or bug or if I can’t solve even then