aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App.pm')
-rw-r--r--perllib/FixMyStreet/App.pm83
1 files changed, 66 insertions, 17 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm
index 5e0bbaf93..c9286b177 100644
--- a/perllib/FixMyStreet/App.pm
+++ b/perllib/FixMyStreet/App.pm
@@ -207,7 +207,8 @@ sub setup_request {
# XXX Put in cobrand / do properly
if ($c->cobrand->moniker eq 'zurich') {
- FixMyStreet::DB::Result::Problem->visible_states_add_unconfirmed();
+ FixMyStreet::DB::Result::Problem->visible_states_add('unconfirmed');
+ FixMyStreet::DB::Result::Problem->visible_states_remove('investigating');
}
if (FixMyStreet->test_mode) {
@@ -318,15 +319,15 @@ sub send_email {
]
};
+ return if $c->is_abuser($vars->{to});
+
# render the template
my $content = $c->view('Email')->render( $c, $template, $vars );
# create an email - will parse headers out of content
my $email = Email::Simple->new($content);
- $email->header_set( ucfirst($_), $vars->{$_} )
- for grep { $vars->{$_} } qw( to from subject Reply-To);
-
- return if $c->is_abuser( $email->header('To') );
+ $email->header_set( 'Subject', $vars->{subject} ) if $vars->{subject};
+ $email->header_set( 'Reply-To', $vars->{'Reply-To'} ) if $vars->{'Reply-To'};
$email->header_set( 'Message-ID', sprintf('<fms-%s-%s@%s>',
time(), unpack('h*', random_bytes(5, 1)), $c->config->{EMAIL_DOMAIN}
@@ -339,10 +340,16 @@ sub send_email {
_template_ => $email->body, # will get line wrapped
_parameters_ => {},
_line_indent => '',
+ From => $vars->{from},
+ To => $vars->{to},
$email->header_pairs
}
) };
+ if (my $attachments = $extra_stash_values->{attachments}) {
+ $email_text = munge_attachments($email_text, $attachments);
+ }
+
# send the email
$c->model('EmailSend')->send($email_text);
@@ -359,17 +366,7 @@ sub send_email_cron {
$params->{From} = [ $sender, _($sender_name) ];
}
- my $first_to;
- if (ref($params->{To}) eq 'ARRAY') {
- if (ref($params->{To}[0]) eq 'ARRAY') {
- $first_to = $params->{To}[0][0];
- } else {
- $first_to = $params->{To}[0];
- }
- } else {
- $first_to = $params->{To};
- }
- return 1 if $c->is_abuser($first_to);
+ return 1 if $c->is_abuser($params->{To});
$params->{'Message-ID'} = sprintf('<fms-cron-%s-%s@%s>', time(),
unpack('h*', random_bytes(5, 1)), FixMyStreet->config('EMAIL_DOMAIN')
@@ -403,8 +400,12 @@ sub send_email_cron {
$params->{_parameters_}->{site_name} = $site_name;
$params->{_line_indent} = '';
+ my $attachments = delete $params->{attachments};
+
my $email = mySociety::Locale::in_gb_locale { mySociety::Email::construct_email($params) };
+ $email = munge_attachments($email, $attachments) if $attachments;
+
if ($nomail) {
print $email;
return 1; # Failure
@@ -418,6 +419,44 @@ sub send_email_cron {
}
}
+sub munge_attachments {
+ my ($message, $attachments) = @_;
+ # $attachments should be an array_ref of things that can be parsed to Email::MIME,
+ # for example
+ # [
+ # body => $binary_data,
+ # attributes => {
+ # content_type => 'image/jpeg',
+ # encoding => 'base64',
+ # filename => '1234.1.jpeg',
+ # name => '1234.1.jpeg',
+ # },
+ # ...
+ # ]
+ #
+ # XXX: mySociety::Email::construct_email isn't using a MIME library and
+ # requires more analysis to refactor, so for now, we'll simply parse the
+ # generated MIME and add attachments.
+ #
+ # (Yes, this means that the email is constructed by Email::Simple, munged
+ # manually by custom code, turned back into Email::Simple, and then munged
+ # with Email::MIME. What's your point?)
+
+ require Email::MIME;
+ my $mime = Email::MIME->new($message);
+ $mime->parts_add([ map { Email::MIME->create(%$_)} @$attachments ]);
+ my $data = $mime->as_string;
+
+ # unsure why Email::MIME adds \r\n. Possibly mail client should handle
+ # gracefully, BUT perhaps as the segment constructed by
+ # mySociety::Email::construct_email strips to \n, they seem not to.
+ # So we re-run the same regexp here to the added part.
+ $data =~ s/\r\n/\n/gs;
+
+ return $data;
+}
+
+
=head2 uri_with
$uri = $c->uri_with( ... );
@@ -533,7 +572,17 @@ sub get_photo_params {
}
sub is_abuser {
- my ($c, $email) = @_;
+ my ($c, $to) = @_;
+ my $email;
+ if (ref($to) eq 'ARRAY') {
+ if (ref($to->[0]) eq 'ARRAY') {
+ $email = $to->[0][0];
+ } else {
+ $email = $to->[0];
+ }
+ } else {
+ $email = $to;
+ }
my ($domain) = $email =~ m{ @ (.*) \z }x;
return $c->model('DB::Abuse')->search( { email => [ $email, $domain ] } )->first;
}