+ if ($tmpdir) {
+ # If we got a writable tmpdir, write to disk
+ push ( @{ $self->{'AttachmentDirs'} ||= [] }, $tmpdir );
+ $parser->output_dir($tmpdir);
+ $parser->filer->ignore_filename(1);
+
+ # Set up the prefix for files with auto-generated names:
+ $parser->output_prefix("part");
+
+ # From the MIME::Parser docs:
+ # "Normally, tmpfiles are created when needed during parsing, and destroyed automatically when they go out of scope"
+ # Turns out that the default is to recycle tempfiles
+ # Temp files should never be recycled, especially when running under perl taint checking
+
+ $parser->tmp_recycling(0) if $parser->can('tmp_recycling');
+ } else {
+ # Otherwise, fall back to storing it in memory
+ $parser->output_to_core(1);
+ $parser->tmp_to_core(1);
+ $parser->use_inner_files(1);
+ }
+
+}
+
+=head2 ParseEmailAddress string
+
+Returns a list of Email::Address objects
+Works around the bug that Email::Address 1.889 and earlier
+doesn't handle local-only email addresses (when users pass
+in just usernames on the RT system in fields that expect
+Email Addresses)
+
+We don't handle the case of
+bob, fred@bestpractical.com
+because we don't want to fail parsing
+bob, "Falcone, Fred" <fred@bestpractical.com>
+The next release of Email::Address will have a new method
+we can use that removes the bandaid
+
+=cut
+
+sub ParseEmailAddress {
+ my $self = shift;
+ my $address_string = shift;
+
+ $address_string =~ s/^\s+|\s+$//g;
+
+ my @addresses;
+ # if it looks like a username / local only email
+ if ($address_string !~ /@/ && $address_string =~ /^\w+$/) {
+ my $user = RT::User->new( RT->SystemUser );
+ my ($id, $msg) = $user->Load($address_string);
+ if ($id) {
+ push @addresses, Email::Address->new($user->Name,$user->EmailAddress);
+ } else {
+ $RT::Logger->error("Unable to parse an email address from $address_string: $msg");
+ }
+ } else {
+ @addresses = Email::Address->parse($address_string);
+ }
+
+ $self->CleanupAddresses(@addresses);
+
+ return @addresses;
+
+}
+
+=head2 CleanupAddresses ARRAY
+
+Massages an array of L<Email::Address> objects to make their email addresses
+more palatable.
+
+Currently this strips off surrounding single quotes around C<< ->address >> and
+B<< modifies the L<Email::Address> objects in-place >>.
+
+Returns the list of objects for convienence in C<map>/C<grep> chains.
+
+=cut
+
+sub CleanupAddresses {
+ my $self = shift;
+
+ for my $addr (@_) {
+ next unless defined $addr;
+ # Outlook sometimes sends addresses surrounded by single quotes;
+ # clean them all up
+ if ((my $email = $addr->address) =~ s/^'(.+)'$/$1/) {
+ $addr->address($email);
+ }
+ }
+ return @_;
+}
+
+=head2 RescueOutlook