From 986dbf736ac34369e43799007b9e57f72e9f20b7 Mon Sep 17 00:00:00 2001 From: LucasGabrielAntonete Date: Wed, 15 Nov 2023 05:23:21 -0800 Subject: [PATCH 1/5] fix in core/urls.py --- core/urls.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/core/urls.py b/core/urls.py index b6bf71b..fc15aea 100644 --- a/core/urls.py +++ b/core/urls.py @@ -7,16 +7,5 @@ urlpatterns = [ path("admin/", admin.site.urls), path("api/", include(usuario_router.urls)), - path('send-email/', SendEmailView.as_view(), name='send-email'), + path('send-email/', SendEmailView.as_view(), name='email'), ] - - -data = { - 'subject': 'Assunto e-mail', - 'message': 'Corpo do e-mail', - 'recipient_email': 'lucasantonete@gmail.com' - -} - -response = requests.post('http://link-aplicação/send-email/', data=data) -print(response.status_code, response.content) From 4e6d841f59b4b380501242d7b15b7c27a6c45650 Mon Sep 17 00:00:00 2001 From: LucasGabrielAntonete Date: Wed, 15 Nov 2023 05:26:48 -0800 Subject: [PATCH 2/5] fix in core/urls-2 --- FabricaClass/urls.py | 0 core/urls.py | 4 +--- 2 files changed, 1 insertion(+), 3 deletions(-) create mode 100644 FabricaClass/urls.py diff --git a/FabricaClass/urls.py b/FabricaClass/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/core/urls.py b/core/urls.py index fc15aea..0498a7e 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,11 +1,9 @@ from django.contrib import admin from django.urls import include,path from usuario.router import router as usuario_router -from FabricaClass.views import SendEmailView -import requests urlpatterns = [ path("admin/", admin.site.urls), path("api/", include(usuario_router.urls)), - path('send-email/', SendEmailView.as_view(), name='email'), + path('api/', include('envioEmail.urls')) ] From 277f1f1954e4dbfd4bb31296d64b396e116e3a8b Mon Sep 17 00:00:00 2001 From: LucasGabrielAntonete Date: Wed, 15 Nov 2023 07:16:49 -0800 Subject: [PATCH 3/5] Feat 6 (#6) envio de emails, criado o app EnvioEmails --- .vscode/settings.json | 5 +++++ EnvioEmails/__init__.py | 0 EnvioEmails/admin.py | 3 +++ EnvioEmails/apps.py | 6 ++++++ EnvioEmails/migrations/__init__.py | 0 EnvioEmails/models.py | 3 +++ EnvioEmails/tests.py | 3 +++ EnvioEmails/urls.py | 6 ++++++ EnvioEmails/views.py | 18 ++++++++++++++++++ FabricaClass/router.py | 0 FabricaClass/serializers.py | 0 FabricaClass/views.py | 19 ------------------- core/settings.py | 9 +++++---- core/urls.py | 4 +++- pdm.lock | 2 +- pyproject.toml | 1 + requirements.txt | 5 +++++ 17 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 EnvioEmails/__init__.py create mode 100644 EnvioEmails/admin.py create mode 100644 EnvioEmails/apps.py create mode 100644 EnvioEmails/migrations/__init__.py create mode 100644 EnvioEmails/models.py create mode 100644 EnvioEmails/tests.py create mode 100644 EnvioEmails/urls.py create mode 100644 EnvioEmails/views.py create mode 100644 FabricaClass/router.py create mode 100644 FabricaClass/serializers.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5857eb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.analysis.extraPaths": [ + "./__pypackages__/3.11/lib" + ] +} \ No newline at end of file diff --git a/EnvioEmails/__init__.py b/EnvioEmails/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/EnvioEmails/admin.py b/EnvioEmails/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/EnvioEmails/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/EnvioEmails/apps.py b/EnvioEmails/apps.py new file mode 100644 index 0000000..f3c1487 --- /dev/null +++ b/EnvioEmails/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EnvioemailsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "EnvioEmails" diff --git a/EnvioEmails/migrations/__init__.py b/EnvioEmails/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/EnvioEmails/models.py b/EnvioEmails/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/EnvioEmails/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/EnvioEmails/tests.py b/EnvioEmails/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/EnvioEmails/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/EnvioEmails/urls.py b/EnvioEmails/urls.py new file mode 100644 index 0000000..5805545 --- /dev/null +++ b/EnvioEmails/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import EmailAPIView + +urlpatterns = [ + path('email/', EmailAPIView.as_view(), name='email'), +] \ No newline at end of file diff --git a/EnvioEmails/views.py b/EnvioEmails/views.py new file mode 100644 index 0000000..b771b86 --- /dev/null +++ b/EnvioEmails/views.py @@ -0,0 +1,18 @@ +from django.shortcuts import render +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status +from django.core.mail import send_mail + +class EmailAPIView(APIView): + def post(self, request, *args, **kwargs): + subject = request.data.get('subject', '') + message = request.data.get('message', '') + recipient_list = request.data.get('recipient_list', '') + + try: + send_mail(subject, message, 'suporte.fabricaclass@gmail.com', recipient_list, fail_silently=False) + return Response({'message': 'Email enviado com sucesso'}, status=status.HTTP_200_OK) + except Exception as e: + return Response({'message': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + \ No newline at end of file diff --git a/FabricaClass/router.py b/FabricaClass/router.py new file mode 100644 index 0000000..e69de29 diff --git a/FabricaClass/serializers.py b/FabricaClass/serializers.py new file mode 100644 index 0000000..e69de29 diff --git a/FabricaClass/views.py b/FabricaClass/views.py index 7c4f08a..e69de29 100644 --- a/FabricaClass/views.py +++ b/FabricaClass/views.py @@ -1,19 +0,0 @@ -from django.shortcuts import render - -from rest_framework import status -from rest_framework.response import Response -from rest_framework.views import APIView -from django.core.mail import send_mail -from django.conf import settings - -class SendEmailView(APIView): - def post(self, request): - subject = request.data.get('subject') - message = request.data.get('message') - from_email = settings.EMAIL_HOST_USER - recipient_list = [request.data.get('recipient_email')] - - send_mail(subject, message, from_email, recipient_list, fail_silently=False) - - return Response({"message": "E-mail enviado com sucesso!"}, status=status.HTTP_200_OK) - diff --git a/core/settings.py b/core/settings.py index ec18846..7fe13be 100644 --- a/core/settings.py +++ b/core/settings.py @@ -28,6 +28,7 @@ "rest_framework", "usuario", "FabricaClass", + 'EnvioEmails', ] @@ -118,8 +119,8 @@ AUTH_USER_MODEL = "usuario.Usuario" EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' -EMAIL_HOST = 'Suporte.FabricaClass@gmail.com' -EMAIL_PORT = 587 +EMAIL_HOST = 'smtp.live.com' +EMAIL_PORT = 25 EMAIL_USE_TLS = True -EMAIL_HOST_USER = 'Suporte.FabricaClass@gmail.com' -EMAIL_HOST_PASSWORD = 'FabricaClass2023Fabrica' \ No newline at end of file +EMAIL_HOST_USER = 'lucasantonete@hotmail.com' +EMAIL_HOST_PASSWORD = '' \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 0498a7e..480e605 100644 --- a/core/urls.py +++ b/core/urls.py @@ -2,8 +2,10 @@ from django.urls import include,path from usuario.router import router as usuario_router + + urlpatterns = [ path("admin/", admin.site.urls), path("api/", include(usuario_router.urls)), - path('api/', include('envioEmail.urls')) + path('emails/', include('EnvioEmails.urls')), ] diff --git a/pdm.lock b/pdm.lock index 90b1875..4b84a93 100644 --- a/pdm.lock +++ b/pdm.lock @@ -6,7 +6,7 @@ groups = ["default", "dev"] cross_platform = true static_urls = false lock_version = "4.3" -content_hash = "sha256:25c376a5e846f61437e6328eabc40c57bb901272349b4db7b05b026d7b2eb016" +content_hash = "sha256:c0b9d0fe05658be76e2d589d617a8912b3551936ec5cace78b6bec6566789f71" [[package]] name = "asgiref" diff --git a/pyproject.toml b/pyproject.toml index 46f847b..879d4df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ license = {text = "MIT"} [tool.pdm.dev-dependencies] dev = [ "black>=23.7.0", + "djangorestframework>=3.14.0", ] [[tool.pdm.autoexport]] diff --git a/requirements.txt b/requirements.txt index 23d8ed0..0f91c53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,9 +2,14 @@ # Please do not edit it manually. asgiref==3.7.2 +certifi==2023.7.22 +charset-normalizer==3.3.2 django==4.2.5 djangorestframework==3.14.0 +idna==3.4 pytz==2023.3.post1 +requests==2.31.0 setuptools==68.2.2 sqlparse==0.4.4 tzdata==2023.3 +urllib3==2.0.7 From 8828c6919ecb5913c08c28213a92ec6cdb2c3bab Mon Sep 17 00:00:00 2001 From: LucasGabrielAntonete Date: Wed, 15 Nov 2023 07:22:16 -0800 Subject: [PATCH 4/5] (#6) fix in settings.py to send emails with gmail port 587 --- core/settings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/settings.py b/core/settings.py index 7fe13be..be4a260 100644 --- a/core/settings.py +++ b/core/settings.py @@ -119,8 +119,8 @@ AUTH_USER_MODEL = "usuario.Usuario" EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' -EMAIL_HOST = 'smtp.live.com' -EMAIL_PORT = 25 +EMAIL_HOST = 'smtp.gmail.com' +EMAIL_PORT = 587 EMAIL_USE_TLS = True -EMAIL_HOST_USER = 'lucasantonete@hotmail.com' +EMAIL_HOST_USER = 'suporte.fabricaclass@gmail.com' EMAIL_HOST_PASSWORD = '' \ No newline at end of file From 4d31dab2d801d32d99aa2c75c30e62b99e171b8e Mon Sep 17 00:00:00 2001 From: LucasGabrielAntonete Date: Fri, 17 Nov 2023 17:01:45 -0300 Subject: [PATCH 5/5] fix to send email (#6) --- EnvioEmails/views.py | 33 +++++++++++++++++++++++---------- core/settings.py | 4 ++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/EnvioEmails/views.py b/EnvioEmails/views.py index b771b86..374cc4e 100644 --- a/EnvioEmails/views.py +++ b/EnvioEmails/views.py @@ -1,18 +1,31 @@ from django.shortcuts import render + +# Create your views here. from rest_framework.views import APIView -from rest_framework.response import Response from rest_framework import status -from django.core.mail import send_mail +from django.core.mail import BadHeaderError, send_mail +from django.core.exceptions import ValidationError +from django.http import HttpResponse + class EmailAPIView(APIView): def post(self, request, *args, **kwargs): - subject = request.data.get('subject', '') - message = request.data.get('message', '') - recipient_list = request.data.get('recipient_list', '') + subject = request.POST.get("subject", "teste") + print(subject) + message = request.POST.get("message", "testando") + recipient_list = ["lucasantonete@hotmail.com", "suporte.fabricaclass@gmail.com"] try: - send_mail(subject, message, 'suporte.fabricaclass@gmail.com', recipient_list, fail_silently=False) - return Response({'message': 'Email enviado com sucesso'}, status=status.HTTP_200_OK) - except Exception as e: - return Response({'message': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - \ No newline at end of file + send_mail( + subject, + message, + recipient_list=recipient_list, + from_email="suporte.fabricaclass@gmail.com", + ) + except BadHeaderError: + return HttpResponse("Invalid header found.") + except ValidationError as e: + return HttpResponse(str(e)) + return HttpResponse( + {"message": "Email enviado com sucesso"}, status=status.HTTP_200_OK + ) \ No newline at end of file diff --git a/core/settings.py b/core/settings.py index be4a260..a249002 100644 --- a/core/settings.py +++ b/core/settings.py @@ -122,5 +122,5 @@ EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_USE_TLS = True -EMAIL_HOST_USER = 'suporte.fabricaclass@gmail.com' -EMAIL_HOST_PASSWORD = '' \ No newline at end of file +EMAIL_HOST_USER = "suporte.fabricaclass@gmail.com" +EMAIL_HOST_PASSWORD = "acsz ygvt prep unkw" \ No newline at end of file