Skip to content

Commit 39faf77

Browse files
committed
Fallback to noreply from address if sender and receiver of notification emails are equal
1 parent 3260edc commit 39faf77

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

changes/TI-3214.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fallback to noreply from address if sender and receiver of notification emails are equal. [elioschmutz]

opengever/activity/mailer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,16 @@ def prepare_mail(self, subject=u'', to_userid=None, to_email=None,
9898
noreply_gever_address = api.portal.get().email_from_address
9999
from_mail = noreply_gever_address
100100

101+
if to_userid:
102+
to_email = ogds_service().fetch_user(to_userid).email
103+
101104
send_with_actor_from_address = api.portal.get_registry_record(
102105
name='send_with_actor_from_address', interface=IOGMailSettings)
103106

104107
if actor:
105-
if not send_with_actor_from_address:
108+
is_same_sender_and_receiver = actor.email == to_email
109+
use_actors_email_for_from = send_with_actor_from_address and not is_same_sender_and_receiver
110+
if not use_actors_email_for_from:
106111
# Set From: header to full name of actor, but 'noreply' address
107112
# of the GEVER deployment. Sending mails with the From-address of
108113
# the actor would lead to them getting rejected in modern mail
@@ -124,9 +129,6 @@ def prepare_mail(self, subject=u'', to_userid=None, to_email=None,
124129
msg['From'] = make_addr_header(
125130
self.default_addr_header, noreply_gever_address, 'utf-8')
126131

127-
if to_userid:
128-
to_email = ogds_service().fetch_user(to_userid).email
129-
130132
recipients = []
131133
if to_email:
132134
msg['To'] = to_email

opengever/activity/tests/test_mail.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from opengever.activity.roles import TASK_RESPONSIBLE_ROLE
99
from opengever.base.interfaces import IOGMailSettings
1010
from opengever.globalindex.model.task import Task
11+
from opengever.ogds.models.user_settings import UserSettings
1112
from opengever.task.activities import TaskAddedActivity
1213
from opengever.testing import IntegrationTestCase
1314
from plone import api
@@ -179,6 +180,34 @@ def test_from_address_with_send_with_actor_from_address_enabled(self, browser):
179180
mail = email.message_from_string(Mailing(self.portal).pop())
180181
self.assertEquals('Ziegler Robert <[email protected]>', get_header(mail, 'From'))
181182

183+
@browsing
184+
def test_from_address_fallback_if_notify_own_action_and_send_with_actor_from_address_enabled(self, browser):
185+
UserSettings.save_setting_for_user(
186+
self.regular_user.getId(), 'notify_own_actions', True)
187+
188+
api.portal.set_registry_record(
189+
'send_with_actor_from_address', True, IOGMailSettings)
190+
191+
self.login(self.dossier_responsible, browser)
192+
self.create_task_via_browser(browser)
193+
process_mail_queue()
194+
195+
# It uses the email address of the currently logged in user to send
196+
# an email, if the recipients email is different.
197+
mail = email.message_from_string(Mailing(self.portal).pop())
198+
self.assertEquals('Ziegler Robert <[email protected]>', get_header(mail, 'From'))
199+
self.assertEquals('[email protected]', get_header(mail, 'To'))
200+
201+
self.login(self.regular_user, browser)
202+
self.create_task_via_browser(browser)
203+
process_mail_queue()
204+
205+
# But it uses the portal email address, if the senders email is the same
206+
# as the recipient email address. This avoids spam filters blocking the email.
207+
mail = email.message_from_string(Mailing(self.portal).pop())
208+
self.assertEquals('B\xc3\xa4rfuss K\xc3\xa4thi <test@localhost>', get_header(mail, 'From'))
209+
self.assertEquals('[email protected]', get_header(mail, 'To'))
210+
182211
@browsing
183212
def test_task_title_is_linked_to_resolve_notification_view(self, browser):
184213
self.login(self.dossier_responsible, browser)

0 commit comments

Comments
 (0)