macropin/django-registration

IndexError: string index out of range when try to send email

msfernandes opened this issue · 2 comments

Hi!
I'm using django==2.0.3 and django-registration-redux==2.2 but I'm having some troubles when I try to create an inactive user with activation email. Here's the code:

# BaseRegistrationView
    def register(self, form):
        site = get_current_site(self.request)

        if hasattr(form, 'save'):
            new_user_instance = form.save()
        else:
            new_user_instance = (UserModel().objects
                                 .create_user(**form.cleaned_data))

        profile = UserProfile.objects.get(user=new_user_instance)
        profile.uf = form.cleaned_data['uf']
        profile.country = form.cleaned_data['country']
        profile.birthdate = form.cleaned_data['birthdate']
        profile.gender = form.cleaned_data['gender']
        profile.save()

        new_user = self.registration_profile.objects.create_inactive_user(
            new_user=new_user_instance,
            site=site,
            send_email=self.SEND_ACTIVATION_EMAIL,
            request=self.request,
        )
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=self.request)
        return new_user

And here's the traceback:

Traceback (most recent call last):
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/msfernandes/Documents/repositories/edem2/src/apps/accounts/views.py", line 31, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/registration/views.py", line 53, in dispatch
    return super(RegistrationView, self).dispatch(request, *args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/views/generic/edit.py", line 142, in post
    return self.form_valid(form)
  File "/home/msfernandes/Documents/repositories/edem2/src/apps/accounts/views.py", line 46, in form_valid
    response = super().form_valid(form)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/registration/views.py", line 56, in form_valid
    new_user = self.register(form)
  File "/home/msfernandes/Documents/repositories/edem2/src/apps/accounts/views.py", line 85, in register
    request=self.request,
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/registration/models.py", line 192, in create_inactive_user
    registration_profile.send_activation_email(site, request)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/registration/models.py", line 442, in send_activation_email
    email_message.send()
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/mail/message.py", line 294, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 110, in send_messages
    sent = self._send(message)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 122, in _send
    from_email = sanitize_address(email_message.from_email, encoding)
  File "/home/msfernandes/Documents/virtualenvs/edem2-uxZmX-r-/lib/python3.6/site-packages/django/core/mail/message.py", line 119, in sanitize_address
    address = Address(nm, addr_spec=addr)
  File "/usr/lib64/python3.6/email/headerregistry.py", line 42, in __init__
    a_s, rest = parser.get_addr_spec(addr_spec)
  File "/usr/lib64/python3.6/email/_header_value_parser.py", line 1577, in get_addr_spec
    token, value = get_local_part(value)
  File "/usr/lib64/python3.6/email/_header_value_parser.py", line 1389, in get_local_part
    if value[0] in CFWS_LEADER:
IndexError: string index out of range

I tried to send an email using only the django backend and works fine

from django.core import mail
mail.send_mail('subject', 'content', 'Name <from@email.com>', ['to@email.com']) # with real emails

Do you have any idea of what can be?

Hey! Sorry for the delay, is this still an issue for you? I think that the bug is updated in django 2.0.4

So if you're able to update (was just released on April 2nd) that should help with the error messaging. But I think that the issue was sending to an empty recipient. Can you confirm that the user you registered had a valid email?

Closing for now. Please feel free to reopen with more information.