Skip to content

Commit

Permalink
Merge pull request #2 from fabricadesoftware-ifc/Envio-de-emails-#6
Browse files Browse the repository at this point in the history
Envio de emails #6
  • Loading branch information
LucasGabrielAntonete authored Nov 19, 2023
2 parents 076546d + 4d31dab commit 62a8837
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.analysis.extraPaths": [
"./__pypackages__/3.11/lib"
]
}
Empty file added EnvioEmails/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions EnvioEmails/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions EnvioEmails/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class EnvioemailsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "EnvioEmails"
Empty file.
3 changes: 3 additions & 0 deletions EnvioEmails/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions EnvioEmails/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
6 changes: 6 additions & 0 deletions EnvioEmails/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from .views import EmailAPIView

urlpatterns = [
path('email/', EmailAPIView.as_view(), name='email'),
]
31 changes: 31 additions & 0 deletions EnvioEmails/views.py
Original file line number Diff line number Diff line change
@@ -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 = ["[email protected]", "[email protected]"]

try:
send_mail(
subject,
message,
recipient_list=recipient_list,
from_email="[email protected]",
)
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
)
Empty file added FabricaClass/router.py
Empty file.
Empty file added FabricaClass/serializers.py
Empty file.
Empty file added FabricaClass/urls.py
Empty file.
19 changes: 0 additions & 19 deletions FabricaClass/views.py
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 4 additions & 3 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"rest_framework",
"usuario",
"FabricaClass",
'EnvioEmails',

]

Expand Down Expand Up @@ -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'
EMAIL_HOST_USER = "suporte.fabricaclass@gmail.com"
EMAIL_HOST_PASSWORD = "acsz ygvt prep unkw"
17 changes: 3 additions & 14 deletions core/urls.py
Original file line number Diff line number Diff line change
@@ -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': '[email protected]'

}

response = requests.post('http://link-aplicação/send-email/', data=data)
print(response.status_code, response.content)
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ license = {text = "MIT"}
[tool.pdm.dev-dependencies]
dev = [
"black>=23.7.0",
"djangorestframework>=3.14.0",
]

[[tool.pdm.autoexport]]
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 62a8837

Please sign in to comment.