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..374cc4e --- /dev/null +++ b/EnvioEmails/views.py @@ -0,0 +1,31 @@ +from django.shortcuts import render + +# Create your views here. +from rest_framework.views import APIView +from rest_framework import status +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.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, + 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/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/urls.py b/FabricaClass/urls.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..a249002 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_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_USE_TLS = True -EMAIL_HOST_USER = 'Suporte.FabricaClass@gmail.com' -EMAIL_HOST_PASSWORD = 'FabricaClass2023Fabrica' \ 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 diff --git a/core/urls.py b/core/urls.py index b6bf71b..480e605 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,22 +1,11 @@ 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='send-email'), + path('emails/', include('EnvioEmails.urls')), ] - - -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) 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