Skip to content

Commit

Permalink
feat(forms): add action to send mail
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Sep 9, 2024
1 parent 7b0e1b9 commit b9727aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/contrib/bonde/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Register your models here.

from django.utils.translation import gettext_lazy as _
from django.core.mail import EmailMultiAlternatives
from django.template import Context, Template

from djangocms_form_builder import actions

from .api import create_form_entry
Expand Down Expand Up @@ -33,3 +36,51 @@ def execute(self, form, request):

if len(list(filter(lambda x: not x, settings.values()))) == 0:
create_form_entry(settings, **form.cleaned_data)



@actions.register
class IntegrateWithEmail(actions.FormAction):
verbose_name = _("Integração com o EMAIL")

class Meta:
entangled_fields = {
"action_parameters": ["subject", "to_email"]
}

# name = forms.CharField(label="Nome", required=False)
to_email = forms.EmailField(label="Email", required=False)
subject = forms.CharField(label="Assunto", required=False)
body = forms.CharField(label="Corpo do e-mail", required=False, widget=forms.Textarea)

def execute(self, form, request):
# name = self.get_parameter(form, "name")
to_email = self.get_parameter(form, "to_email")
subject_text = self.get_parameter(form, "subject")
body_text = self.get_parameter(form, "body")

# context = form.cleaned_data
# email_template_name = "forms/body.html"
# html_email_template_name = email_template_name
# import ipdb;ipdb.set_trace()
# if to_email and body_text and subject_text:
"""
Send a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
from_email = form.cleaned_data.get("email")
context = Context({**form.cleaned_data})
# subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
# subject = "".join(subject.splitlines())

subject = Template(subject_text).render(context)
body = Template(body_text).render(context)


email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
email_message.attach_alternative(body, "text/html")
email_message.send()

# TODO:
# Adicionar campos do formulário no contexto do assunto
#
Empty file.

0 comments on commit b9727aa

Please sign in to comment.