diff --git a/remo/base/tasks.py b/remo/base/tasks.py index 37860c04c..179c9397e 100644 --- a/remo/base/tasks.py +++ b/remo/base/tasks.py @@ -58,12 +58,19 @@ def send_remo_mail(subject, recipients_list, sender=None, if email_template: message = render_to_string(email_template, data) - if not sender: - email = EmailMessage(subject=subject, body=message, - from_email=settings.FROM_EMAIL, - to=[to], headers=headers) - else: - email = EmailMessage(subject=subject, body=message, - from_email=sender, to=[to], cc=[sender], - headers=headers) - email.send() + email_data = { + 'subject': subject, + 'body': message, + 'from_email': settings.FROM_EMAIL, + 'to': [to], + } + + if sender: + # If there is a sender, add a Reply-To header and send a copy to the sender + headers.update({'Reply-To': sender}) + email_data.update({'cc': [sender]}) + + # Add the headers to the mail data + email_data.update({'headers': headers}) + # Send the email + EmailMessage(**email_data).send() diff --git a/remo/base/tests/test_tasks.py b/remo/base/tests/test_tasks.py index 79ab724ad..c74d784e6 100644 --- a/remo/base/tests/test_tasks.py +++ b/remo/base/tests/test_tasks.py @@ -26,8 +26,8 @@ def test_base(self): to=[to], cc=[from_email], subject=subject, - headers={}, - from_email=from_email) + headers={'Reply-To': from_email}, + from_email=settings.FROM_EMAIL) def test_send_email_from_remobot(self): recipient = UserFactory.create() @@ -75,8 +75,8 @@ def test_send_to_valid_email_address(self): to=['mail@example.com'], cc=[from_email], subject=subject, - headers={}, - from_email=from_email) + headers={'Reply-To': from_email}, + from_email=settings.FROM_EMAIL) def test_send_to_invalid_email_address(self): subject = 'This is the subject' @@ -102,5 +102,5 @@ def test_headers(self): to=['mail@example.com'], cc=[from_email], subject=subject, - from_email=from_email, + from_email=settings.FROM_EMAIL, headers=headers) diff --git a/remo/profiles/models.py b/remo/profiles/models.py index 84e463f86..bb33cb339 100644 --- a/remo/profiles/models.py +++ b/remo/profiles/models.py @@ -415,12 +415,12 @@ def email_mentor_notification(sender, instance, raw, **kwargs): subject=subject, email_template=email_template, data=ctx_data, - headers={'Reply-To': instance.user.email}) + sender=instance.user.email) send_remo_mail.delay(recipients_list=rep_recipient, subject=subject, email_template=email_template, data=ctx_data, - headers={'Reply-To': instance.mentor.email}) + sender=instance.mentor.email) statsd.incr('profiles.change_mentor') diff --git a/remo/profiles/tests/test_models.py b/remo/profiles/tests/test_models.py index beed5c188..4931afa87 100644 --- a/remo/profiles/tests/test_models.py +++ b/remo/profiles/tests/test_models.py @@ -329,8 +329,6 @@ def test_send_email_on_mentor_change(self, mocked_mail): data2 = mocked_mail.call_args_list[1][1] recipients = data1['recipients_list'] + data2['recipients_list'] eq_(set(recipients), set([new_mentor.id, old_mentor.id, user.id])) - eq_(data1['headers']['Reply-To'], user.email) - eq_(data2['headers']['Reply-To'], new_mentor.email) class UserStatusNotification(RemoTestCase): diff --git a/remo/reports/tests/test_tasks.py b/remo/reports/tests/test_tasks.py index 9fcbb2456..adecd4738 100644 --- a/remo/reports/tests/test_tasks.py +++ b/remo/reports/tests/test_tasks.py @@ -100,10 +100,10 @@ def test_base(self): eq_(mail_mock.call_count, 2) expected_call_list = [ - call(rep_subject, [rep.email], message=mockany, - headers={'Reply-To': mentor.email}), - call(mentor_subject, [mentor.email], message=mockany, - headers={'Reply-To': rep.email})] + call(subject=rep_subject, recipients_list=[rep.email], message=mockany, + sender=mentor.email), + call(subject=mentor_subject, recipients_list=[mentor.email], message=mockany, + sender=rep.email)] eq_(mail_mock.call_args_list, expected_call_list) def test_with_report_filled(self): @@ -132,10 +132,11 @@ def test_with_no_report_filled_and_one_notification(self): eq_(mail_mock.call_count, 2) expected_call_list = [ - call(rep_subject, [rep.email], message=mockany, - headers={'Reply-To': mentor.email}), - call(mentor_subject, [mentor.email], message=mockany, - headers={'Reply-To': rep.email})] + call(subject=rep_subject, recipients_list=[rep.email], message=mockany, + sender=mentor.email), + call(subject=mentor_subject, recipients_list=[mentor.email], message=mockany, + sender=rep.email) + ] eq_(mail_mock.call_args_list, expected_call_list) def test_with_user_unavailable(self): diff --git a/remo/reports/utils.py b/remo/reports/utils.py index 60c3a46f6..b920e5359 100644 --- a/remo/reports/utils.py +++ b/remo/reports/utils.py @@ -63,10 +63,11 @@ def send_report_notification(reps, weeks): rep_message = render_to_string(rep_mail_body, ctx_data) mentor_message = render_to_string(mentor_mail_body, ctx_data) if mentor: - send_remo_mail(rep_subject, [rep.email], message=rep_message, - headers={'Reply-To': mentor.email}) - send_remo_mail(mentor_subject, [mentor.email], - message=mentor_message, - headers={'Reply-To': rep.email}) + send_remo_mail(subject=rep_subject, recipients_list=[rep.email], + sender=mentor.email, message=rep_message) + send_remo_mail(subject=mentor_subject, + recipients_list=[mentor.email], + sender=rep.email, + message=mentor_message) else: send_remo_mail(rep_subject, [rep.email], message=rep_message)