Here's the code that I use :

with mail.get_connection() as connection:
        connection.open()
send_mail(Subject ,Message, Email, recipient_list, connection=connection)
        connection.close()
Works beautifully, it is available at django docs



[image: --]

Maninder Kumar
[image: http://]about.me/maninder.s.kumar
<http://about.me/maninder.s.kumar?promo=email_sig>




On Fri, Jan 17, 2020 at 2:24 AM Jose Chacón <josejc...@gmail.com> wrote:

> Hello guys , i've came across to this error while using django email in a
> method which is fired after a post request to register new users . The goal
> of this method is to after a user is created,  an email must be sent to
> user's mail in order to finish registration workflow. Every time the 
> msg.send()
> is executed  the following message gets printed in the terminal
>
> [16/Jan/2020 11:15:28] "POST /api/v1/auth/usuario/ HTTP/1.1" 400 54
> ----------------------------------------
> Exception happened during processing of request from ('127.0.0.1', 58382)
> Traceback (most recent call last):
>   File
> "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py",
> line 650, in process_request_thread
>     self.finish_request(request, client_address)
>   File
> "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py",
> line 360, in finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File
> "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py",
> line 720, in __init__
>     self.handle()
>   File
> "D:\JITZONE\JitZone\venv\lib\site-packages\django\core\servers\basehttp.py",
> line 174, in handle
>     self.handle_one_request()
>   File
> "D:\JITZONE\JitZone\venv\lib\site-packages\django\core\servers\basehttp.py",
> line 182, in handle_one_request
>     self.raw_requestline = self.rfile.readline(65537)
>   File
> "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socket.py",
> line 669, in readinto
>     return self._sock.recv_into(b)
> ConnectionAbortedError: [WinError 10053] An established connection was
> aborted by the software in your host machine
>
> Although doesn't affect functionality (email still get sent) , it's
> bugging me  because this error makes the POST request canceled .
> Here are my methods:
>
>
> *1.views.py <http://1.views.py> *
>
>
> class ProfileCreateAPIView(CreateAPIView):
>     queryset = User.objects.all()
>     model = UsuarioPerfil
>     serializer_class = UserSerializer
>
> *2.serializers.py <http://2.serializers.py>*
>
>
>
> class UserSerializer(serializers.ModelSerializer):
>     usuario_perfil = UsuarioPerfilSerializer()
>     generar_correo_confirmacion = serializers.CharField(required=False)
>     enviar_correo = serializers.CharField(required=False)
>
>     class Meta:
>         model = User
>         fields = ['password', 'email', 'username', 'first_name', 'last_name', 
> 'usuario_perfil',
>                   'generar_correo_confirmacion', 'enviar_correo']
>
>     def create(self, validated_data):
>
>         #######################CREATION OF USER AND 
> USERPROFILE#####################################
>
>         profile_data = validated_data.pop('usuario_perfil')
>         user = User.objects.create(
>             username=validated_data['username'],
>             email=validated_data['email'],
>             first_name=validated_data["first_name"],
>             last_name=validated_data["last_name"],
>             is_active=False
>         )
>         user.set_password(validated_data['password'])
>         user.save()
>         UsuarioPerfil.objects.create(
>             usuario=user,
>             perfil=perfil,
>             red_social=red_social,
>             **profile_data)
>
>         ###########CONFIRMATION 
> EMAIL###############################################################
>         token = account_validation_token.make_token(user)
>         user_id = urlsafe_base64_encode(force_bytes(user.id))
>         url = 'http://localhost:8000' + 
> reverse('usuarios:confirmacionReinicioPassword',
>                                                 kwargs={'user_id': user_*id*, 
> 'token': token})
>
>         try:
>             htmly = get_template('mail/recuperacion_contrasenia.html')
>             d = {'confirm_url': url}
>             subject, from_email, to = 'Confirmacion de cuenta', 
> settings.EMAIL_HOST_USER, user.email
>             html_content = htmly.render(d)
>             msg = EmailMultiAlternatives(subject, html_content, from_email, 
> [to])
>             msg.attach_alternative(html_content, "text / html")
>             msg.send()
>         except:
>             print(":c")
>
>         return user
>
>
> What it's confusing me its that there's no trace of this error if i send
> an email in a View which gets fired after a GET request.  Also i've tried
> putting the logic in a post method in django view , without using Django
> Rest Framework,  but the error still appears
>
> I'd be so grateful if you help me. Thanks in advance
> José Chacón
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d1cdb3b2-0f25-4ce5-b66e-3bbdb66e0cac%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d1cdb3b2-0f25-4ce5-b66e-3bbdb66e0cac%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABOHK3Rin3TBjx-May7kC%3Di9K%3DESoVm2zbMrR8dO%2Bcky00fC-Q%40mail.gmail.com.

Reply via email to