Skip to content

Commit

Permalink
Merge pull request #4905 from open-formulieren/issue/4895-outgoing-em…
Browse files Browse the repository at this point in the history
…ail-logging-improvements

[#4895] outgoing email logging improvements
  • Loading branch information
sergei-maertens authored Dec 12, 2024
2 parents 92aa9c0 + 9441219 commit 0dd2174
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/openforms/emails/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class EmailEventChoices(models.TextChoices):
registration = "registration", _("Registration")
confirmation = "confirmation", _("Confirmation")
cosign_confirmation = "cosign_confirmation", _("Co-sign confirmation")
cosign_request = "cosign_request", _("Co-sign request")


class EmailContentTypeChoices(models.TextChoices):
submission = "submission.submission", _("Submission")
submission = "submissions.Submission", _("Submission")
13 changes: 13 additions & 0 deletions src/openforms/submissions/tasks/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

from openforms.celery import app
from openforms.config.models import GlobalConfiguration
from openforms.emails.constants import (
X_OF_CONTENT_TYPE_HEADER,
X_OF_CONTENT_UUID_HEADER,
X_OF_EVENT_HEADER,
EmailContentTypeChoices,
EmailEventChoices,
)
from openforms.emails.utils import send_mail_html
from openforms.frontend import get_frontend_redirect_url
from openforms.logging import logevent
Expand Down Expand Up @@ -133,6 +140,12 @@ def send_email_cosigner(submission_id: int) -> None:
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[recipient],
text_message=content,
extra_headers={
"Content-Language": submission.language_code,
X_OF_CONTENT_TYPE_HEADER: EmailContentTypeChoices.submission,
X_OF_CONTENT_UUID_HEADER: str(submission.uuid),
X_OF_EVENT_HEADER: EmailEventChoices.cosign_request,
},
)
except Exception:
logevent.cosigner_email_queuing_failure(submission)
Expand Down
32 changes: 32 additions & 0 deletions src/openforms/submissions/tests/test_task_co_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.test import TestCase

from openforms.config.models import GlobalConfiguration
from openforms.emails.constants import EmailContentTypeChoices, EmailEventChoices
from openforms.logging.models import TimelineLogProxy

from ..tasks import send_email_cosigner
Expand Down Expand Up @@ -175,3 +176,34 @@ def test_form_link_not_allowed_in_email(self):

self.assertNotIn(form_link, email.body)
self.assertIn(submission.public_registration_reference, email.body)

def test_headers_in_cosign_request_email(self):
submission = SubmissionFactory.from_components(
components_list=[
{
"key": "cosign",
"type": "cosign",
"label": "Cosign component",
},
],
submitted_data={"cosign": "[email protected]"},
form_url="http://testserver/myform/",
)
send_email_cosigner(submission.id)

self.assertEqual(len(mail.outbox), 1)
message = mail.outbox[0]

# UUID is not a constant, so just test if it exists
submission_uuid = message.extra_headers.pop("X-OF-Content-UUID", None)
self.assertIsNotNone(submission_uuid)

# Test remaining headers
self.assertEqual(
message.extra_headers,
{
"Content-Language": "nl",
"X-OF-Content-Type": EmailContentTypeChoices.submission,
"X-OF-Event": EmailEventChoices.cosign_request,
},
)
32 changes: 32 additions & 0 deletions src/openforms/submissions/tests/test_tasks_confirmation_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from privates.test import temp_private_root

from openforms.config.models import GlobalConfiguration
from openforms.emails.constants import EmailContentTypeChoices, EmailEventChoices
from openforms.emails.models import ConfirmationEmailTemplate
from openforms.emails.tests.factories import ConfirmationEmailTemplateFactory
from openforms.forms.tests.factories import FormStepFactory
Expand Down Expand Up @@ -641,6 +642,37 @@ def test_template_is_rendered_in_submission_language(self):
self.assertIn("Translated Radio option 1", html_message)
self.assertIn("Translated Select option 2", html_message)

def test_headers_present_in_confirmation_email(self):
submission = SubmissionFactory.from_components(
completed=True,
components_list=[
{
"key": "email",
"confirmationRecipient": True,
},
],
submitted_data={"email": "[email protected]"},
)
with override_settings(CELERY_TASK_ALWAYS_EAGER=True):
send_confirmation_email(submission.id)

self.assertEqual(len(mail.outbox), 1)
message = mail.outbox[0]

# UUID is not a constant, so just test if it exists
submission_uuid = message.extra_headers.pop("X-OF-Content-UUID", None)
self.assertIsNotNone(submission_uuid)

# Test remaining headers
self.assertEqual(
message.extra_headers,
{
"Content-Language": "nl",
"X-OF-Content-Type": EmailContentTypeChoices.submission,
"X-OF-Event": EmailEventChoices.confirmation,
},
)


class RaceConditionTests(TransactionTestCase):
@patch("openforms.submissions.tasks.emails._send_confirmation_email")
Expand Down
13 changes: 13 additions & 0 deletions src/openforms/submissions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
get_confirmation_email_context_data,
get_confirmation_email_templates,
)
from openforms.emails.constants import (
X_OF_CONTENT_TYPE_HEADER,
X_OF_CONTENT_UUID_HEADER,
X_OF_EVENT_HEADER,
EmailContentTypeChoices,
EmailEventChoices,
)
from openforms.emails.utils import (
render_email_template,
send_mail_html,
Expand Down Expand Up @@ -204,6 +211,12 @@ def send_confirmation_email(submission: Submission) -> None:
cc=cc_emails,
text_message=text_content,
theme=submission.form.theme,
extra_headers={
"Content-Language": submission.language_code,
X_OF_CONTENT_TYPE_HEADER: EmailContentTypeChoices.submission,
X_OF_CONTENT_UUID_HEADER: str(submission.uuid),
X_OF_EVENT_HEADER: EmailEventChoices.confirmation,
},
)
except Exception as e:
logevent.confirmation_email_failure(submission, e)
Expand Down

0 comments on commit 0dd2174

Please sign in to comment.