diff options
author | Hakim Cassimally <hakim@mysociety.org> | 2015-03-03 17:52:05 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2015-10-06 09:09:23 +0100 |
commit | 597019d4fc28d160588d137ac58d948393f26af2 (patch) | |
tree | a35d552f83b962bc7b54ac0f3f5cb113251f2795 /perllib | |
parent | 014d2a4342d1dbe7d2987376974b20116439e07d (diff) |
Allow attachment of emails in email_send
Required by Zurich for mysociety/FixMyStreet-Commercial#675
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App.pm | 46 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 20 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Email.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 18 |
4 files changed, 89 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index cd05dcb8f..c5d628ab6 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -345,6 +345,10 @@ sub send_email { } ) }; + if (my $attachments = $extra_stash_values->{attachments}) { + $email_text = munge_attachments($email_text, $attachments); + } + # send the email $c->model('EmailSend')->send($email_text); @@ -395,8 +399,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 @@ -410,6 +418,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( ... ); diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index fa6eb060b..5d75b62dc 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -71,7 +71,25 @@ sub _jpeg_magic { # and \x{49}\x{49} (Tiff, 3 results in live DB) ? } -has images => ( # jpeg data for actual image +=head2 C<images>, C<num_images>, C<get_raw_image_data>, C<all_images> + +C<$photoset-E<GT>images> is an AoA containing the filed and the binary image data. + + [ + [ $fileid1, $binary_data ], + [ $fileid2, $binary_data ], + ... + ] + +Various accessors are provided onto it: + + num_images: count + get_raw_image_data ($index): return the [$fileid, $binary_data] tuple + all_images: return AoA as an array (e.g. rather than arrayref) + +=cut + +has images => ( # AoA of [$fileid, $binary_data] tuples isa => 'ArrayRef', is => 'rw', traits => ['Array'], diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm index 9fec0ac9c..bac408510 100644 --- a/perllib/FixMyStreet/SendReport/Email.pm +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -93,6 +93,11 @@ sub send { To => $self->to, From => $self->send_from( $row ), }; + + my $app = FixMyStreet::App->new( cobrand => $cobrand ); + + $cobrand->munge_sendreport_params($app, $row, $h, $params) if $cobrand->can('munge_sendreport_params'); + $params->{Bcc} = $self->bcc if @{$self->bcc}; if (FixMyStreet::Email::test_dmarc($params->{From}[0])) { @@ -100,7 +105,7 @@ sub send { $params->{From} = [ mySociety::Config::get('CONTACT_EMAIL'), $params->{From}[1] ]; } - my $result = FixMyStreet::App->send_email_cron( + my $result = $app->send_email_cron( $params, mySociety::Config::get('CONTACT_EMAIL'), $nomail, diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index ef60b0d36..8325b07a8 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -221,6 +221,24 @@ sub get_email { return $emails[0]; } +=head2 get_first_email + + $email = $mech->get_first_email(@emails); + +Returns first email in queue as a string and fails a test if the mail doesn't have a date and epoch-containing Message-ID header. + +=cut + +sub get_first_email { + my $mech = shift; + my $email = shift or do { fail 'No email retrieved'; return }; + my $email_as_string = $email->as_string; + ok $email_as_string =~ s{\s+Date:\s+\S.*?$}{}xmsg, "Found and stripped out date"; + ok $email_as_string =~ s{\s+Message-ID:\s+\S.*?$}{}xmsg, "Found and stripped out message ID (contains epoch)"; + return $email_as_string; +} + + =head2 page_errors my $arrayref = $mech->page_errors; |