+=head2
+
+ $header = handle_escaped_commas('','From: ')
+
+Handle commas in addresses which have been RFC1522 escaped and now need to be
+quoted to avoid parsing as a record separator.
+
+=cut
+
+sub handle_escaped_commas {
+ my ($modified_hdr, $orig_hdr) = @_;
+
+ my $field = Mail::Message::Field->new($orig_hdr);
+ # if the header isn't structured, it can't contain an address
+ if (not $field->isStructured()) {
+ return $modified_hdr
+ }
+ if ($field->name() !~ m/^(?:to|from|reply-to)$/) {
+ return $modified_hdr
+ }
+ my @addresses = $field->addresses();
+ if (not @addresses) {
+ return $modified_hdr
+ }
+ my @return_addresses;
+ for my $address (@addresses) {
+ $address->phrase(decode_rfc1522($address->phrase()));
+ push @return_addresses, $address->format();
+ }
+ return join(', ',@return_addresses)
+}
+