aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App.pm
diff options
context:
space:
mode:
authorHakim Cassimally <hakim@mysociety.org>2015-03-03 17:52:05 +0000
committerDave Arter <davea@mysociety.org>2015-10-06 09:09:23 +0100
commit597019d4fc28d160588d137ac58d948393f26af2 (patch)
treea35d552f83b962bc7b54ac0f3f5cb113251f2795 /perllib/FixMyStreet/App.pm
parent014d2a4342d1dbe7d2987376974b20116439e07d (diff)
Allow attachment of emails in email_send
Required by Zurich for mysociety/FixMyStreet-Commercial#675
Diffstat (limited to 'perllib/FixMyStreet/App.pm')
-rw-r--r--perllib/FixMyStreet/App.pm46
1 files changed, 46 insertions, 0 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( ... );