# Cannot Send email

**URL:** <https://forum.djangoproject.com/t/cannot-send-email/24423>\
**Category:** Forms & APIs\
**Created:** [October 9, 2023, 3:48pm UTC](https://forum.djangoproject.com/t/cannot-send-email/24423 "2023-10-09T15:48:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PARTHIB-DEB](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/parthib-deb/32/16351_2.png) [@PARTHIB-DEB](https://forum.djangoproject.com/u/PARTHIB-DEB)\
**Post date:** [October 9, 2023, 3:48pm UTC](https://forum.djangoproject.com/t/cannot-send-email/24423/1 "2023-10-09T15:48:17Z")

</div>

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?

```auto
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!!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 9, 2023, 9:00pm UTC](https://forum.djangoproject.com/t/cannot-send-email/24423/2 "2023-10-09T21:00:11Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![PARTHIB-DEB](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/parthib-deb/32/16351_2.png) [@PARTHIB-DEB](https://forum.djangoproject.com/u/PARTHIB-DEB)\
**Post date:** [October 10, 2023, 4:04am UTC](https://forum.djangoproject.com/t/cannot-send-email/24423/3 "2023-10-10T04:04:12Z")

</div>

```bash
# 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'

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 10, 2023, 4:22am UTC](https://forum.djangoproject.com/t/cannot-send-email/24423/4 "2023-10-10T04:22:22Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![PARTHIB-DEB](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/parthib-deb/32/16351_2.png) [@PARTHIB-DEB](https://forum.djangoproject.com/u/PARTHIB-DEB)\
**Post date:** [October 10, 2023, 4:39am UTC](https://forum.djangoproject.com/t/cannot-send-email/24423/5 "2023-10-10T04:39:48Z")

</div>

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