Skip to content

Commit

Permalink
feat: custom send mail method
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroHenriqueDevBR committed Jan 24, 2024
1 parent d7341bc commit d8f54b9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 296 deletions.
268 changes: 4 additions & 264 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,267 +126,7 @@ dmypy.json

# Pyre type checker
.pyre/
ldap_password/static_volume/admin/css/autocomplete.css
ldap_password/static_volume/admin/css/base.css
ldap_password/static_volume/admin/css/changelists.css
ldap_password/static_volume/admin/css/dark_mode.css
ldap_password/static_volume/admin/css/dashboard.css
ldap_password/static_volume/admin/css/forms.css
ldap_password/static_volume/admin/css/login.css
ldap_password/static_volume/admin/css/nav_sidebar.css
ldap_password/static_volume/admin/css/responsive_rtl.css
ldap_password/static_volume/admin/css/responsive.css
ldap_password/static_volume/admin/css/rtl.css
ldap_password/static_volume/admin/css/widgets.css
ldap_password/static_volume/admin/css/vendor/select2/LICENSE-SELECT2.md
ldap_password/static_volume/admin/css/vendor/select2/select2.css
ldap_password/static_volume/admin/css/vendor/select2/select2.min.css
ldap_password/static_volume/admin/img/calendar-icons.svg
ldap_password/static_volume/admin/img/icon-addlink.svg
ldap_password/static_volume/admin/img/icon-alert.svg
ldap_password/static_volume/admin/img/icon-calendar.svg
ldap_password/static_volume/admin/img/icon-changelink.svg
ldap_password/static_volume/admin/img/icon-clock.svg
ldap_password/static_volume/admin/img/icon-deletelink.svg
ldap_password/static_volume/admin/img/icon-no.svg
ldap_password/static_volume/admin/img/icon-unknown-alt.svg
ldap_password/static_volume/admin/img/icon-unknown.svg
ldap_password/static_volume/admin/img/icon-viewlink.svg
ldap_password/static_volume/admin/img/icon-yes.svg
ldap_password/static_volume/admin/img/inline-delete.svg
ldap_password/static_volume/admin/img/LICENSE
ldap_password/static_volume/admin/img/README.txt
ldap_password/static_volume/admin/img/search.svg
ldap_password/static_volume/admin/img/selector-icons.svg
ldap_password/static_volume/admin/img/sorting-icons.svg
ldap_password/static_volume/admin/img/tooltag-add.svg
ldap_password/static_volume/admin/img/tooltag-arrowright.svg
ldap_password/static_volume/admin/img/gis/move_vertex_off.svg
ldap_password/static_volume/admin/img/gis/move_vertex_on.svg
ldap_password/static_volume/admin/js/actions.js
ldap_password/static_volume/admin/js/autocomplete.js
ldap_password/static_volume/admin/js/calendar.js
ldap_password/static_volume/admin/js/cancel.js
ldap_password/static_volume/admin/js/change_form.js
ldap_password/static_volume/admin/js/collapse.js
ldap_password/static_volume/admin/js/core.js
ldap_password/static_volume/admin/js/filters.js
ldap_password/static_volume/admin/js/inlines.js
ldap_password/static_volume/admin/js/jquery.init.js
ldap_password/static_volume/admin/js/nav_sidebar.js
ldap_password/static_volume/admin/js/popup_response.js
ldap_password/static_volume/admin/js/prepopulate_init.js
ldap_password/static_volume/admin/js/prepopulate.js
ldap_password/static_volume/admin/js/SelectBox.js
ldap_password/static_volume/admin/js/SelectFilter2.js
ldap_password/static_volume/admin/js/theme.js
ldap_password/static_volume/admin/js/urlify.js
ldap_password/static_volume/admin/js/admin/DateTimeShortcuts.js
ldap_password/static_volume/admin/js/admin/RelatedObjectLookups.js
ldap_password/static_volume/admin/js/vendor/jquery/jquery.js
ldap_password/static_volume/admin/js/vendor/jquery/jquery.min.js
ldap_password/static_volume/admin/js/vendor/jquery/LICENSE.txt
ldap_password/static_volume/admin/js/vendor/select2/LICENSE.md
ldap_password/static_volume/admin/js/vendor/select2/select2.full.js
ldap_password/static_volume/admin/js/vendor/select2/select2.full.min.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/af.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ar.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/az.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/bg.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/bn.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/bs.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ca.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/cs.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/da.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/de.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/dsb.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/el.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/en.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/es.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/et.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/eu.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/fa.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/fi.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/fr.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/gl.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/he.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/hi.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/hr.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/hsb.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/hu.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/hy.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/id.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/is.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/it.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ja.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ka.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/km.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ko.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/lt.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/lv.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/mk.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ms.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/nb.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ne.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/nl.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/pl.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ps.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/pt-BR.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/pt.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ro.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/ru.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sk.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sl.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sq.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sr-Cyrl.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sr.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/sv.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/th.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/tk.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/tr.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/uk.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/vi.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/zh-CN.js
ldap_password/static_volume/admin/js/vendor/select2/i18n/zh-TW.js
ldap_password/static_volume/admin/js/vendor/xregexp/LICENSE.txt
ldap_password/static_volume/admin/js/vendor/xregexp/xregexp.js
ldap_password/static_volume/admin/js/vendor/xregexp/xregexp.min.js
ldap_password/static_volume/css/bootstrap.min.css
ldap_password/static_volume/css/custom.css
ldap_password/static_volume/css/font-awesome-all.min.css
ldap_password/static_volume/images/logo.png
ldap_password/static_volume/js/bootstrap.bundle.min.js
ldap_password/static_volume/js/jquery.min.js
ldap_password/staticfiles/admin/css/autocomplete.css
ldap_password/staticfiles/admin/css/base.css
ldap_password/staticfiles/admin/css/changelists.css
ldap_password/staticfiles/admin/css/dark_mode.css
ldap_password/staticfiles/admin/css/dashboard.css
ldap_password/staticfiles/admin/css/forms.css
ldap_password/staticfiles/admin/css/login.css
ldap_password/staticfiles/admin/css/nav_sidebar.css
ldap_password/staticfiles/admin/css/responsive_rtl.css
ldap_password/staticfiles/admin/css/responsive.css
ldap_password/staticfiles/admin/css/rtl.css
ldap_password/staticfiles/admin/css/widgets.css
ldap_password/staticfiles/admin/css/vendor/select2/LICENSE-SELECT2.md
ldap_password/staticfiles/admin/css/vendor/select2/select2.css
ldap_password/staticfiles/admin/css/vendor/select2/select2.min.css
ldap_password/staticfiles/admin/img/calendar-icons.svg
ldap_password/staticfiles/admin/img/icon-addlink.svg
ldap_password/staticfiles/admin/img/icon-alert.svg
ldap_password/staticfiles/admin/img/icon-calendar.svg
ldap_password/staticfiles/admin/img/icon-changelink.svg
ldap_password/staticfiles/admin/img/icon-clock.svg
ldap_password/staticfiles/admin/img/icon-deletelink.svg
ldap_password/staticfiles/admin/img/icon-no.svg
ldap_password/staticfiles/admin/img/icon-unknown-alt.svg
ldap_password/staticfiles/admin/img/icon-unknown.svg
ldap_password/staticfiles/admin/img/icon-viewlink.svg
ldap_password/staticfiles/admin/img/icon-yes.svg
ldap_password/staticfiles/admin/img/inline-delete.svg
ldap_password/staticfiles/admin/img/LICENSE
ldap_password/staticfiles/admin/img/README.txt
ldap_password/staticfiles/admin/img/search.svg
ldap_password/staticfiles/admin/img/selector-icons.svg
ldap_password/staticfiles/admin/img/sorting-icons.svg
ldap_password/staticfiles/admin/img/tooltag-add.svg
ldap_password/staticfiles/admin/img/tooltag-arrowright.svg
ldap_password/staticfiles/admin/img/gis/move_vertex_off.svg
ldap_password/staticfiles/admin/img/gis/move_vertex_on.svg
ldap_password/staticfiles/admin/js/actions.js
ldap_password/staticfiles/admin/js/autocomplete.js
ldap_password/staticfiles/admin/js/calendar.js
ldap_password/staticfiles/admin/js/cancel.js
ldap_password/staticfiles/admin/js/change_form.js
ldap_password/staticfiles/admin/js/collapse.js
ldap_password/staticfiles/admin/js/core.js
ldap_password/staticfiles/admin/js/filters.js
ldap_password/staticfiles/admin/js/inlines.js
ldap_password/staticfiles/admin/js/jquery.init.js
ldap_password/staticfiles/admin/js/nav_sidebar.js
ldap_password/staticfiles/admin/js/popup_response.js
ldap_password/staticfiles/admin/js/prepopulate_init.js
ldap_password/staticfiles/admin/js/prepopulate.js
ldap_password/staticfiles/admin/js/SelectBox.js
ldap_password/staticfiles/admin/js/SelectFilter2.js
ldap_password/staticfiles/admin/js/theme.js
ldap_password/staticfiles/admin/js/urlify.js
ldap_password/staticfiles/admin/js/admin/DateTimeShortcuts.js
ldap_password/staticfiles/admin/js/admin/RelatedObjectLookups.js
ldap_password/staticfiles/admin/js/vendor/jquery/jquery.js
ldap_password/staticfiles/admin/js/vendor/jquery/jquery.min.js
ldap_password/staticfiles/admin/js/vendor/jquery/LICENSE.txt
ldap_password/staticfiles/admin/js/vendor/select2/LICENSE.md
ldap_password/staticfiles/admin/js/vendor/select2/select2.full.js
ldap_password/staticfiles/admin/js/vendor/select2/select2.full.min.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/af.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ar.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/az.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/bg.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/bn.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/bs.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ca.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/cs.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/da.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/de.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/dsb.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/el.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/en.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/es.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/et.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/eu.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/fa.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/fi.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/fr.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/gl.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/he.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/hi.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/hr.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/hsb.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/hu.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/hy.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/id.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/is.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/it.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ja.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ka.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/km.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ko.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/lt.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/lv.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/mk.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ms.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/nb.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ne.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/nl.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/pl.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ps.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/pt-BR.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/pt.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ro.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/ru.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sk.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sl.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sq.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sr-Cyrl.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sr.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/sv.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/th.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/tk.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/tr.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/uk.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/vi.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/zh-CN.js
ldap_password/staticfiles/admin/js/vendor/select2/i18n/zh-TW.js
ldap_password/staticfiles/admin/js/vendor/xregexp/LICENSE.txt
ldap_password/staticfiles/admin/js/vendor/xregexp/xregexp.js
ldap_password/staticfiles/admin/js/vendor/xregexp/xregexp.min.js
ldap_password/staticfiles/css/bootstrap.min.css
ldap_password/staticfiles/css/custom.css
ldap_password/staticfiles/css/font-awesome-all.min.css
ldap_password/staticfiles/images/logo.png
ldap_password/staticfiles/js/bootstrap.bundle.min.js
ldap_password/staticfiles/js/jquery.min.js
ldap_password/dockerfiles/nginx/cert/ca.cer
ldap_password/dockerfiles/nginx/cert/ca.key
ldap_password/static_volume/*
ldap_password/staticfiles/*
ldap_password/dockerfiles/nginx/cert/*
ldap_password/temp/*
102 changes: 89 additions & 13 deletions ldap_password/apps/core/services/mail/send_mail.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,91 @@
from django.conf import settings
from django.core.mail import send_mail

# from django.core.mail import send_mail
import smtplib
import email.utils
from email.message import EmailMessage
import ssl


class CustomMailBackend:
def send_mail(self, subject: str, message: str, to: str) -> None:
SENDERNAME = "Não Responda"
RECIPIENT = to
SENDER = settings.DEFAULT_FROM_EMAIL
USERNAME_SMTP = settings.EMAIL_HOST_USER
password_smtp = settings.EMAIL_HOST_PASSWORD
HOST = settings.EMAIL_HOST
PORT = settings.EMAIL_PORT
SUBJECT = subject

# The email body for recipients with non-HTML email clients.
BODY_TEXT = (
"Defensoria Pública do Estado do Piauí\r\n"
+ "Esse é um e-mail confidencial, não o repasse para ninguém.\n"
+ message
)

# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Defensoria Pública do Estado do Piauí</h1>
<p>
Esse é um e-mail confidencial, não o repasse para ninguém.<br>
{}
</p>
</body>
</html>""".format(
message
)

msg = EmailMessage()
msg["Subject"] = SUBJECT
msg["From"] = email.utils.formataddr((SENDERNAME, SENDER))
msg["To"] = RECIPIENT

msg.add_alternative(BODY_TEXT, subtype="text")
msg.add_alternative(BODY_HTML, subtype="html")

try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls(
context=ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH, cafile=None, capath=None
)
)
server.ehlo()
server.login(USERNAME_SMTP, password_smtp)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
except Exception as e:
print(f"Error: {e}")
else:
print("Email successfully sent!")


class MailService:
def __init__(self) -> None:
self.mail_service = CustomMailBackend()

def send_token(self, to: str, token: str) -> None:
subject = "NO REPPLY"
mail_body = "Token para recuperação de senha: {}".format(token)
send_mail(
self.mail_service.send_mail(
subject=subject,
message=mail_body,
from_email=settings.EMAIL_HOST_USER,
recipient_list=[
to,
],
fail_silently=False,
to=to,
)
# send_mail(
# subject=subject,
# message=mail_body,
# from_email=settings.EMAIL_HOST_USER,
# recipient_list=[
# to,
# ],
# fail_silently=False,
# )

def send_password(self, to: str, password: str) -> None:
subject = "NO REPPLY"
Expand All @@ -23,12 +94,17 @@ def send_password(self, to: str, password: str) -> None:
password,
)
)
send_mail(
self.mail_service.send_mail(
subject=subject,
message=mail_body,
from_email=settings.EMAIL_HOST_USER,
recipient_list=[
to,
],
fail_silently=False,
to=to,
)
# send_mail(
# subject=subject,
# message=mail_body,
# from_email=settings.EMAIL_HOST_USER,
# recipient_list=[
# to,
# ],
# fail_silently=False,
# )
Loading

0 comments on commit d8f54b9

Please sign in to comment.