Permalink
Browse files

Have a handlemail script to ignore certain emails before forwarding on.

  • Loading branch information...
1 parent 9a8601a commit a2aac34ac9bb1e7f49a593d1440092b90a1e5041 @dracos dracos committed Apr 2, 2013
Showing with 63 additions and 0 deletions.
  1. +63 −0 bin/handlemail-support
View
63 bin/handlemail-support
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -w
+#
+# handlemail-support:
+# Handle an individual incoming mail message.
+#
+# This script should be invoked through the .forward mechanism. It processes
+# emails to the support address to remove out of office and so on, before
+# forwarding on.
+#
+# Copyright (c) 2013 UK Citizens Online Democracy. All rights reserved.
+# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/
+
+use strict;
+require 5.8.0;
+
+# Horrible boilerplate to set up appropriate library paths.
+use FindBin;
+use lib "$FindBin::Bin/../perllib";
+use lib "$FindBin::Bin/../commonlib/perllib";
+
+use mySociety::Config;
+BEGIN {
+ mySociety::Config::set_file("$FindBin::Bin/../conf/general");
+}
+use mySociety::EmailUtil;
+use mySociety::HandleMail;
+
+my %data = mySociety::HandleMail::get_message();
+forward_on() if $data{is_bounce_message};
+exit 0 if is_ignorable($data{message});
+forward_on();
+
+# ---
+
+sub forward_on {
+ my ($l, $d) = split /\@/, mySociety::Config::get('CONTACT_EMAIL');
+ if (mySociety::EmailUtil::EMAIL_SUCCESS
+ != mySociety::EmailUtil::send_email(
+ join("\n", @{$data{lines}}) . "\n",
+ $data{return_path},
+ join('@', join('_deli', $l, 'very'), $d)
+ )) {
+ exit 75;
+ }
+ exit 0;
+}
+
+sub is_ignorable {
+ my $m = shift;
+ my $head = $m->head();
+ my ($from, $subject, $body) = ($head->get('From'), $head->get('Subject'), $m->body);
+ $body = join("\n", @$body);
+
+ open my $fp, "$FindBin::Bin/../../data/ignored-emails.csv" or exit 75;
+ while (<$fp>) {
+ chomp;
+ my ($f, $s, $b) = split /,/;
+ next unless $f || $s || $b;
+ return 1 unless ( $f && $from !~ /$f/ ) || ( $s && $subject !~ /$s/ ) || ( $b && $body !~ /$b/ );
+ }
+ return 0;
+}
+

0 comments on commit a2aac34

Please sign in to comment.