diff --git a/accounts/models.py b/accounts/models.py index 623a6bbe7..b431d3c4b 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -673,13 +673,16 @@ def send_announcement_email(cls, user: User, study, children): return announcement_message def send_as_email(self): + lab_email = self.related_study.lab.contact_email + context = { "base_url": settings.BASE_URL, "custom_message": mark_safe(self.body), + "lab_name": self.related_study.lab.name, + "lab_email": lab_email, + "study_name": self.related_study.name, } - lab_email = self.related_study.lab.contact_email - recipient_email_list = list(self.recipients.values_list("username", flat=True)) for to_email in recipient_email_list: diff --git a/studies/templates/emails/custom_email.html b/studies/templates/emails/custom_email.html index 6c67c5001..a60e52e2a 100644 --- a/studies/templates/emails/custom_email.html +++ b/studies/templates/emails/custom_email.html @@ -1,5 +1,6 @@ {% extends "emails/base.html" %} {% block content %} + {% include "emails/custom_email_header.html" %} {% autoescape off %} {{ custom_message }} {% endautoescape %} diff --git a/studies/templates/emails/custom_email.txt b/studies/templates/emails/custom_email.txt index 818479e53..8b9e2b5c7 100644 --- a/studies/templates/emails/custom_email.txt +++ b/studies/templates/emails/custom_email.txt @@ -1,4 +1,6 @@ {% extends "emails/base.txt" %} + {% block content %} +{% include "emails/custom_email_header.txt" %} {{ custom_message|striptags }} {% endblock content %} diff --git a/studies/templates/emails/custom_email_header.html b/studies/templates/emails/custom_email_header.html new file mode 100644 index 000000000..b5c043297 --- /dev/null +++ b/studies/templates/emails/custom_email_header.html @@ -0,0 +1 @@ +

{% include "emails/custom_email_header.txt" %}

diff --git a/studies/templates/emails/custom_email_header.txt b/studies/templates/emails/custom_email_header.txt new file mode 100644 index 000000000..13cac2e4a --- /dev/null +++ b/studies/templates/emails/custom_email_header.txt @@ -0,0 +1 @@ +This is an email from {{ lab_name }} for "{{ study_name }}". You may reply to this email to message directly with the researchers at {{ lab_name }} or email them at {{ lab_email }} if you have other questions. diff --git a/studies/templates/studies/study_participant_contact.html b/studies/templates/studies/study_participant_contact.html index 4c748c2ad..a5df8df52 100644 --- a/studies/templates/studies/study_participant_contact.html +++ b/studies/templates/studies/study_participant_contact.html @@ -143,6 +143,15 @@ +
+
Email Header
+
+

All particiapant emails will see the following header when they're contacted from this form:

+ + {% include "emails/custom_email_header.html" with lab_name=study.lab.name study_name=study.name lab_email=study.lab.contact_email %} + +
+
{% csrf_token %} {% bootstrap_form form %} diff --git a/studies/tests.py b/studies/tests.py index f74bb1593..92d3283b9 100644 --- a/studies/tests.py +++ b/studies/tests.py @@ -596,7 +596,13 @@ def test_potential_message_targets_external(self): class TestSendMail(TestCase): def setUp(self): - self.context = {"token": G(User).generate_token(), "username": "username"} + self.context = { + "token": G(User).generate_token(), + "username": "username", + "lab_email": "lab_email", + "study_name": "study_name", + "lab_name": "lab_name", + } def test_send_email_with_image(self): email = send_mail( @@ -613,9 +619,8 @@ def test_send_email_with_image(self): self.assertEqual(email.subject, "Test email") self.assertEqual(email.to, ["lookit-test-email@mit.edu"]) self.assertTrue( - email.body.startswith( - "\nline 1[IMAGE]line 2\n\n\nUpdate your email preferences here" - ), + "\nline 1[IMAGE]line 2\n\n\nUpdate your email preferences here" + in email.body, "Email plain text does not have expected substitution of [IMAGE] for image tag", ) self.assertEqual( @@ -627,7 +632,7 @@ def test_send_email_with_image(self): self.assertEqual( email.alternatives[0], ( - f'\n \n

line 1

line 2

\n \n\n
\nUpdate your email preferences\nUnsubscribe from all emails\n', + f'\n

This is an email from {self.context["lab_name"]} for "{self.context["study_name"]}". You may reply to this email to message directly with the researchers at {self.context["lab_name"]} or email them at {self.context["lab_email"]} if you have other questions.\n

\n\n \n

line 1

line 2

\n \n\n
\nUpdate your email preferences\nUnsubscribe from all emails\n', "text/html", ), )