diff options
-rw-r--r-- | perllib/FixMyStreet/SendReport/Email.pm | 4 | ||||
-rw-r--r-- | perllib/Utils/Email.pm | 12 | ||||
-rw-r--r-- | t/app/model/problem.t | 18 | ||||
-rw-r--r-- | t/cobrand/zurich.t | 8 | ||||
-rw-r--r-- | t/cobrand/zurich_attachments.txt | 2 | ||||
-rw-r--r-- | t/utils/email.t | 3 |
6 files changed, 36 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm index 8fedc48b5..9abccf510 100644 --- a/perllib/FixMyStreet/SendReport/Email.pm +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -2,6 +2,7 @@ package FixMyStreet::SendReport::Email; use Moo; use FixMyStreet::Email; +use Utils::Email; BEGIN { extends 'FixMyStreet::SendReport'; } @@ -90,7 +91,8 @@ sub send { my $sender = FixMyStreet::Email::unique_verp_id('report', $row->id); - if (FixMyStreet::Email::test_dmarc($params->{From}[0])) { + if (FixMyStreet::Email::test_dmarc($params->{From}[0]) + || Utils::Email::same_domain($params->{From}, $params->{To})) { $params->{'Reply-To'} = [ $params->{From} ]; $params->{From} = [ $sender, $params->{From}[1] ]; } diff --git a/perllib/Utils/Email.pm b/perllib/Utils/Email.pm index a30e41c61..5e3df0205 100644 --- a/perllib/Utils/Email.pm +++ b/perllib/Utils/Email.pm @@ -34,4 +34,16 @@ sub _send { return grep { $_->type eq $type } @answers; } +sub same_domain { + my ($email, $list) = @_; + my $addr = (Email::Address->parse($email->[0]))[0]; + return unless $addr; + my $domain = $addr->host; + foreach (@$list) { + my $addr = (Email::Address->parse($_->[0]))[0]; + next unless $addr; + return 1 if $domain eq $addr->host; + } +} + 1; diff --git a/t/app/model/problem.t b/t/app/model/problem.t index e9ec22b77..6b1be0a76 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -148,7 +148,7 @@ for my $test ( my $user = FixMyStreet::DB->resultset('User')->find_or_create( { - email => 'system_user@example.com' + email => 'system_user@example.net' } ); @@ -428,7 +428,7 @@ for my $contact ( { }, { body_id => $body_ids{14279}[0], # Ballymoney category => 'Graffiti', - email => 'highways@example.com', + email => 'highways@example.net', }, { confirmed => 0, body_id => $body_ids{2636}, # Isle of Wight @@ -439,7 +439,7 @@ for my $contact ( { } my %common = ( - email => 'system_user@example.com', + email => 'system_user@example.net', name => 'Andrew Smith', ); foreach my $test ( { @@ -505,6 +505,9 @@ foreach my $test ( { body => $body_ids{14279}[0], category => 'Graffiti', longitude => -6.5, + # As Ballmoney contact has same domain as reporter, the From line will + # become a unique reply address and Reply-To will become the reporter + reply_to => 1, }, { %common, desc => 'directs NI correctly, 2', @@ -563,7 +566,12 @@ foreach my $test ( { if ( $test->{ email_count } ) { my $email = $mech->get_email; like $email->header('To'), $test->{ to }, 'to line looks correct'; - is $email->header('From'), sprintf('"%s" <%s>', $test->{ name }, $test->{ email } ), 'from line looks correct'; + if ($test->{reply_to}) { + is $email->header('Reply-To'), sprintf('"%s" <%s>', $test->{ name }, $test->{ email } ), 'Reply-To line looks correct'; + like $email->header('From'), qr/"$test->{name}" <fms-report-\d+-\w+\@example.org>/, 'from line looks correct'; + } else { + is $email->header('From'), sprintf('"%s" <%s>', $test->{ name }, $test->{ email } ), 'from line looks correct'; + } like $email->header('Subject'), qr/A Title/, 'subject line looks correct'; my $body = $mech->get_text_body_from_email($email); like $body, qr/A user of FixMyStreet/, 'email body looks a bit like a report'; @@ -671,7 +679,7 @@ subtest 'check can turn on report sent email alerts' => sub { my $email = $emails[0]; like $email->header('To'),qr/City of Edinburgh Council/, 'to line looks correct'; - is $email->header('From'), '"Test User" <system_user@example.com>', 'from line looks correct'; + is $email->header('From'), '"Test User" <system_user@example.net>', 'from line looks correct'; like $email->header('Subject'), qr/A Title/, 'subject line looks correct'; my $body = $mech->get_text_body_from_email($email); like $body, qr/A user of FixMyStreet/, 'email body looks a bit like a report'; diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 85cada27a..0a84d2d03 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -82,7 +82,7 @@ $subdivision->endpoint( 'subdivision@example.org' ); $subdivision->update; my $external_body = $mech->create_body_ok( 4, 'External Body' ); $external_body->send_method( 'Zurich' ); -$external_body->endpoint( 'external_body@example.org' ); +$external_body->endpoint( 'external_body@example.net' ); $external_body->update; sub get_export_rows_count { @@ -563,7 +563,7 @@ subtest "external report triggers email" => sub { send_reports_for_zurich(); $email = $mech->get_email; like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay'; - like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct'; + like $email->header('To'), qr/external_body\@example.net/, 'to line looks correct'; like $email->body, qr/External Body/, 'body has right name'; like $email->body, qr/$EXTERNAL_MESSAGE/, 'external_message was passed on'; unlike $email->body, qr/test\@example.com/, 'body does not contain email address'; @@ -599,7 +599,7 @@ subtest "external report triggers email" => sub { send_reports_for_zurich(); $email = $mech->get_email; like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay'; - like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct'; + like $email->header('To'), qr/external_body\@example.net/, 'to line looks correct'; like $email->body, qr/External Body/, 'body has right name'; like $email->body, qr/test\@example.com/, 'body does contain email address'; $mech->clear_emails_ok; @@ -630,7 +630,7 @@ subtest "external report triggers email" => sub { send_reports_for_zurich(); $email = $mech->get_email; like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay'; - like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct'; + like $email->header('To'), qr/external_body\@example.net/, 'to line looks correct'; like $email->body, qr/External Body/, 'body has right name'; like $email->body, qr/$EXTERNAL_MESSAGE/, 'external_message was passed on'; like $email->body, qr/test\@example.com/, 'body contains email address'; diff --git a/t/cobrand/zurich_attachments.txt b/t/cobrand/zurich_attachments.txt index bf8d6872b..25a1bacf0 100644 --- a/t/cobrand/zurich_attachments.txt +++ b/t/cobrand/zurich_attachments.txt @@ -1,7 +1,7 @@ MIME-Version: 1.0
Subject: =?iso-8859-1?Q?Z=FCri?= wie neu: Weitergeleitete Meldung #REPORT_ID
Content-Type: multipart/mixed; boundary="BOUNDARY"
-To: "External Body" <external_body@example.org>
+To: "External Body" <external_body@example.net>
Content-Transfer-Encoding: 7bit
From: "FixMyStreet" <division@example.org>
diff --git a/t/utils/email.t b/t/utils/email.t index 23814c1d7..9e556a865 100644 --- a/t/utils/email.t +++ b/t/utils/email.t @@ -31,4 +31,7 @@ is Utils::Email::test_dmarc('BAD'), undef; is Utils::Email::test_dmarc('test@yahoo.com'), 1; is Utils::Email::test_dmarc('test@example.net'), undef; +is Utils::Email::same_domain(['test@example.net', ''], [ ['to@example.net', ''], ['to@example.com', ''] ]), 1; +is Utils::Email::same_domain(['test@example.org', ''], [ ['to@example.net', ''] ]), ''; + done_testing(); |