Search code examples
python-3.xdjango-rest-frameworksmtpovh

Django rest api send email return value true but email isnt sent


I'm trying to send the token to reset password on my django application, the post return a confirmation that the email is sent but, no email has been sent. here are my settings and config

settings :

EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = os.getenv('EMAIL_PORT')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL')

.env :

  EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
    EMAIL_PORT=465
    EMAIL_USE_TLS = True
    EMAIL_HOST="ssl0.ovh.net"
    EMAIL_HOST_USER=""
    EMAIL_HOST_PASSWORD=""
    DEFAULT_FROM_EMAIL=EMAIL_HOST_USER

signals.py:

from django.core.mail import EmailMultiAlternatives, send_mail
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.urls import reverse
from ndgd.settings import EMAIL_HOST_USER
from django_rest_passwordreset.signals import reset_password_token_created
from rest_framework.decorators import api_view, permission_classes

@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
    
    context = {
        'current_user': reset_password_token.user,
        'username': reset_password_token.user.username,
        'email': reset_password_token.user.email,
        'reset_password_url':"{}?token={}".format(
            instance.request.build_absolute_uri(reverse('password_reset:reset-password-confirm')),
            reset_password_token.key)
    }

    # Email
    email_html_message = render_to_string('templates/email/password_reset.email.html', context)
    email_plaintext_message = render_to_string('templates/email/password_reset_email.txt', context)

    ms = EmailMultiAlternatives(
        "Password Reset".format(title = "Nouvelles Donnes"),
        email_plaintext_message,
        EMAIL_HOST_USER,
        [reset_password_token.user.email]
    )

    print(EMAIL_HOST_USER)
    print(reset_password_token.user.email)
    ms.attach_alternative(email_html_message, "text/html")
    ms.send

this is the postman return value but the email is never received i need help. thank you. enter image description here


Solution

  • There was two issues first : like the answer above i had to change ms.send to ms.send(),

    Which raised another issue. for using EMAIL_PORT=465 i had to change EMAIL_USE_TLS = True to EMAIL_USE_SSL = True