<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use 5.008001;
use strict;
use warnings;
package Email::MIME;
# ABSTRACT: easy MIME message handling
$Email::MIME::VERSION = '1.946';
use Email::Simple 2.212; # nth header value
use parent qw(Email::Simple);

use Carp ();
use Email::MessageID;
use Email::MIME::Creator;
use Email::MIME::ContentType 1.022; # parse_content_disposition
use Email::MIME::Encode;
use Email::MIME::Encodings 1.314;
use Email::MIME::Header;
use Email::MIME::Modifier;
use Encode 1.9801 ();
use Scalar::Util qw(reftype weaken);

our @CARP_NOT = qw(Email::MIME::ContentType);

#pod =head1 SYNOPSIS
#pod
#pod B&lt;Wait!&gt;  Before you read this, maybe you just need L&lt;Email::Stuffer&gt;, which is
#pod a much easier-to-use tool for building simple email messages that might have
#pod attachments or both plain text and HTML.  If that doesn't do it for you, then
#pod by all means keep reading.
#pod
#pod   use Email::MIME;
#pod   my $parsed = Email::MIME-&gt;new($message);
#pod
#pod   my @parts = $parsed-&gt;parts; # These will be Email::MIME objects, too.
#pod   my $decoded = $parsed-&gt;body;
#pod   my $non_decoded = $parsed-&gt;body_raw;
#pod
#pod   my $content_type = $parsed-&gt;content_type;
#pod
#pod ...or...
#pod
#pod   use Email::MIME;
#pod   use IO::All;
#pod
#pod   # multipart message
#pod   my @parts = (
#pod       Email::MIME-&gt;create(
#pod           attributes =&gt; {
#pod               filename     =&gt; "report.pdf",
#pod               content_type =&gt; "application/pdf",
#pod               encoding     =&gt; "quoted-printable",
#pod               name         =&gt; "2004-financials.pdf",
#pod           },
#pod           body =&gt; io( "2004-financials.pdf" )-&gt;binary-&gt;all,
#pod       ),
#pod       Email::MIME-&gt;create(
#pod           attributes =&gt; {
#pod               content_type =&gt; "text/plain",
#pod               disposition  =&gt; "attachment",
#pod               charset      =&gt; "US-ASCII",
#pod           },
#pod           body_str =&gt; "Hello there!",
#pod       ),
#pod   );
#pod
#pod   my $email = Email::MIME-&gt;create(
#pod       header_str =&gt; [
#pod           From =&gt; 'casey@geeknest.com',
#pod           To =&gt; [ 'user1@host.com', 'Name &lt;user2@host.com&gt;' ],
#pod           Cc =&gt; Email::Address::XS-&gt;new("Display Name \N{U+1F600}", 'user@example.com'),
#pod       ],
#pod       parts      =&gt; [ @parts ],
#pod   );
#pod
#pod   # nesting parts
#pod   $email-&gt;parts_set(
#pod       [
#pod         $email-&gt;parts,
#pod         Email::MIME-&gt;create( parts =&gt; [ @parts ] ),
#pod       ],
#pod   );
#pod
#pod   # standard modifications
#pod   $email-&gt;header_str_set( 'X-PoweredBy' =&gt; 'RT v3.0'      );
#pod   $email-&gt;header_str_set( To            =&gt; rcpts()        );
#pod   $email-&gt;header_str_set( Cc            =&gt; aux_rcpts()    );
#pod   $email-&gt;header_str_set( Bcc           =&gt; sekrit_rcpts() );
#pod
#pod   # more advanced
#pod   $_-&gt;encoding_set( 'base64' ) for $email-&gt;parts;
#pod
#pod   # Quick multipart creation
#pod   my $email = Email::MIME-&gt;create(
#pod       header_str =&gt; [
#pod           From =&gt; 'my@address',
#pod           To   =&gt; 'your@address',
#pod       ],
#pod       parts =&gt; [
#pod           q[This is part one],
#pod           q[This is part two],
#pod           q[These could be binary too],
#pod       ],
#pod   );
#pod
#pod   print $email-&gt;as_string;
#pod
#pod =head1 DESCRIPTION
#pod
#pod This is an extension of the L&lt;Email::Simple&gt; module, to handle MIME
#pod encoded messages. It takes a message as a string, splits it up into its
#pod constituent parts, and allows you access to various parts of the
#pod message. Headers are decoded from MIME encoding.
#pod
#pod =head1 METHODS
#pod
#pod Please see L&lt;Email::Simple&gt; for the base set of methods. It won't take
#pod very long. Added to that, you have:
#pod
#pod =cut

our $CREATOR = 'Email::MIME::Creator';

my $NO_ENCODE_RE = qr/
  \A
  (?:7bit|8bit|binary)\s*(?:;|$)
/ix;

sub new {
  my ($class, $text, $arg, @rest) = @_;
  $arg ||= {};

  my $encode_check = exists $arg-&gt;{encode_check}
                   ? delete $arg-&gt;{encode_check}
                   : Encode::FB_CROAK;

  my $self = shift-&gt;SUPER::new($text, $arg, @rest);
  $self-&gt;encode_check_set($encode_check);
  $self-&gt;{ct} = parse_content_type($self-&gt;content_type);
  $self-&gt;parts;
  return $self;
}

#pod =method create
#pod
#pod   my $single = Email::MIME-&gt;create(
#pod     header_str =&gt; [ ... ],
#pod     body_str   =&gt; '...',
#pod     attributes =&gt; { ... },
#pod   );
#pod
#pod   my $multi = Email::MIME-&gt;create(
#pod     header_str =&gt; [ ... ],
#pod     parts      =&gt; [ ... ],
#pod     attributes =&gt; { ... },
#pod   );
#pod
#pod This method creates a new MIME part. The C&lt;header_str&gt; parameter is a list of
#pod headers pairs to include in the message. The value for each pair is expected to
#pod be a text string that will be MIME-encoded as needed.  Alternatively it can be
#pod an object with C&lt;as_mime_string&gt; method which implements conversion of that
#pod object to MIME-encoded string.  That object method is called with two named
#pod input parameters: C&lt;charset&gt; and C&lt;header_name_length&gt;.  It should return
#pod MIME-encoded representation of the object.  As of 2017-07-25, the
#pod header-value-as-object code is very young, and may yet change.
#pod
#pod In case header name is registered in C&lt;%Email::MIME::Header::header_to_class_map&gt;
#pod hash then registered class is used for conversion from Unicode string to 8bit
#pod MIME encoding.  Value can be either string or array reference to strings.
#pod Object is constructed via method C&lt;from_string&gt; with string value (or values
#pod in case of array reference) and converted to MIME-encoded string via
#pod C&lt;as_mime_string&gt; method.
#pod
#pod A similar C&lt;header&gt; parameter can be provided in addition to or instead of
#pod C&lt;header_str&gt;.  Its values will be used verbatim.
#pod
#pod C&lt;attributes&gt; is a hash of MIME attributes to assign to the part, and may
#pod override portions of the header set in the C&lt;header&gt; parameter. The hash keys
#pod correspond directly to methods or modifying a message from
#pod C&lt;Email::MIME::Modifier&gt;. The allowed keys are: content_type, charset, name,
#pod format, boundary, encoding, disposition, and filename. They will be mapped to
#pod C&lt;"$attr\_set"&gt; for message modification.
#pod
#pod The C&lt;parts&gt; parameter is a list reference containing C&lt;Email::MIME&gt;
#pod objects. Elements of the C&lt;parts&gt; list can also be a non-reference
#pod string of data. In that case, an C&lt;Email::MIME&gt; object will be created
#pod for you. Simple checks will determine if the part is binary or not, and
#pod all parts created in this fashion are encoded with C&lt;base64&gt;, just in case.
#pod
#pod If C&lt;body&gt; is given instead of C&lt;parts&gt;, it specifies the body to be used for a
#pod flat (subpart-less) MIME message.  It is assumed to be a sequence of octets.
#pod
#pod If C&lt;body_str&gt; is given instead of C&lt;body&gt; or C&lt;parts&gt;, it is assumed to be a
#pod character string to be used as the body.  If you provide a C&lt;body_str&gt;
#pod parameter, you B&lt;must&gt; provide C&lt;charset&gt; and C&lt;encoding&gt; attributes.
#pod
#pod =cut

my %CT_SETTER = map {; $_ =&gt; 1 } qw(
  content_type charset name format boundary
  encoding
  disposition filename
);

sub create {
  my ($class, %args) = @_;

  my $header = '';
  my %headers;
  if (exists $args{header}) {
    my @headers = @{ $args{header} };
    pop @headers if @headers % 2 == 1;
    while (my ($key, $value) = splice @headers, 0, 2) {
      $headers{$key} = 1;
      $CREATOR-&gt;_add_to_header(\$header, $key, $value);
    }
  }

  if (exists $args{header_str}) {
    my @headers = @{ $args{header_str} };
    pop @headers if @headers % 2 == 1;
    while (my ($key, $value) = splice @headers, 0, 2) {
      $headers{$key} = 1;

      $value = Email::MIME::Encode::maybe_mime_encode_header(
          $key, $value, 'UTF-8'
      );
      $CREATOR-&gt;_add_to_header(\$header, $key, $value);
    }
  }

  $CREATOR-&gt;_add_to_header(\$header, Date =&gt; $CREATOR-&gt;_date_header)
    unless exists $headers{Date};
  $CREATOR-&gt;_add_to_header(\$header, 'MIME-Version' =&gt; '1.0',);

  my %attrs = $args{attributes} ? %{ $args{attributes} } : ();

  # XXX: This is awful... but if we don't do this, then Email::MIME-&gt;new will
  # end up calling parse_content_type($self-&gt;content_type) which will mean
  # parse_content_type(undef) which, for some reason, returns the default.
  # It's really sort of mind-boggling.  Anyway, the default ends up being
  # q{text/plain; charset="us-ascii"} so that if content_type is in the
  # attributes, but not charset, then charset isn't changed and you up with
  # something that's q{image/jpeg; charset="us-ascii"} and you look like a
  # moron. -- rjbs, 2009-01-20
  if (
    grep { exists $attrs{$_} } qw(content_type charset name format boundary)
  ) {
    $CREATOR-&gt;_add_to_header(\$header, 'Content-Type' =&gt; 'text/plain',);
  }

  my %pass_on;

  if (exists $args{encode_check}) {
    $pass_on{encode_check} = $args{encode_check};
  }

  my $email = $class-&gt;new($header, \%pass_on);

  for my $key (keys %attrs) {
    $email-&gt;content_type_attribute_set($key =&gt; $attrs{$key});
  }

  my $body_args = grep { defined $args{$_} } qw(parts body body_str);
  Carp::confess("only one of parts, body, or body_str may be given")
    if $body_args &gt; 1;

  if ($args{parts} &amp;&amp; @{ $args{parts} }) {
    foreach my $part (@{ $args{parts} }) {
      $part = $CREATOR-&gt;_construct_part($part)
        unless ref($part);
    }
    $email-&gt;parts_set($args{parts});
  } elsif (defined $args{body}) {
    $email-&gt;body_set($args{body});
  } elsif (defined $args{body_str}) {
    Carp::confess("body_str was given, but no charset is defined")
      unless my $charset = $attrs{charset};

    Carp::confess("body_str was given, but no encoding is defined")
      unless $attrs{encoding};

    my $body_octets = Encode::encode($attrs{charset}, $args{body_str}, $email-&gt;encode_check);
    $email-&gt;body_set($body_octets);
  }

  $email;
}

sub as_string {
  my $self = shift;
  return $self-&gt;__head-&gt;as_string
    . ($self-&gt;{mycrlf} || "\n")  # XXX: replace with -&gt;crlf
    . $self-&gt;body_raw;
}

sub parts {
  my $self = shift;

  $self-&gt;fill_parts unless $self-&gt;{parts};

  my @parts = @{ $self-&gt;{parts} };
  @parts = $self unless @parts;
  return @parts;
}

sub subparts {
  my ($self) = @_;

  $self-&gt;fill_parts unless $self-&gt;{parts};
  my @parts = @{ $self-&gt;{parts} };
  return @parts;
}

sub fill_parts {
  my $self = shift;

  if (
    $self-&gt;{ct}{type} eq "multipart"
    or $self-&gt;{ct}{type} eq "message"
  ) {
    $self-&gt;parts_multipart;
  } else {
    $self-&gt;parts_single_part;
  }

  return $self;
}

sub body {
  my $self = shift;
  my $body = $self-&gt;SUPER::body;
  my $cte  = $self-&gt;header("Content-Transfer-Encoding") || '';

  $cte =~ s/\A\s+//;
  $cte =~ s/\s+\z//;
  $cte =~ s/;.+//; # For S/MIME, etc.

  return $body unless $cte;

  if (!$self-&gt;force_decode_hook and $cte =~ $NO_ENCODE_RE) {
    return $body;
  }

  $body = $self-&gt;decode_hook($body) if $self-&gt;can("decode_hook");

  $body = Email::MIME::Encodings::decode($cte, $body, '7bit');
  return $body;
}

sub parts_single_part {
  my $self = shift;
  $self-&gt;{parts} = [];
  return $self;
}

sub body_raw {
  return $_[0]-&gt;{body_raw} || $_[0]-&gt;SUPER::body;
}

sub body_str {
  my ($self) = @_;
  my $encoding = $self-&gt;{ct}{attributes}{charset};

  unless ($encoding) {
    if ($self-&gt;{ct}{type} eq 'text'
      and
      ($self-&gt;{ct}{subtype} eq 'plain' or $self-&gt;{ct}{subtype} eq 'html')
    ) {

      # assume that plaintext or html without ANY charset is us-ascii
      return $self-&gt;body;
    }

    Carp::confess("can't get body as a string for " . $self-&gt;content_type);
  }

  my $str = Encode::decode($encoding, $self-&gt;body, $self-&gt;encode_check);
  return $str;
}

sub parts_multipart {
  my $self     = shift;
  my $boundary = $self-&gt;{ct}-&gt;{attributes}-&gt;{boundary};

  # Take a message, join all its lines together.  Now try to Email::MIME-&gt;new
  # it with 1.861 or earlier.  Death!  It tries to recurse endlessly on the
  # body, because every time it splits on boundary it gets itself. Obviously
  # that means it's a bogus message, but a mangled result (or exception) is
  # better than endless recursion. -- rjbs, 2008-01-07
  return $self-&gt;parts_single_part
    unless $boundary and $self-&gt;body_raw =~ /^--\Q$boundary\E\s*$/sm;

  $self-&gt;{body_raw} = $self-&gt;SUPER::body;

  # rfc1521 7.2.1
  my ($body, $epilogue) = split /^--\Q$boundary\E--\s*$/sm, $self-&gt;body_raw, 2;

  my @bits = split /^--\Q$boundary\E\s*$/sm, ($body || '');

  $self-&gt;SUPER::body_set(undef);

  # If there are no headers in the potential MIME part, it's just part of the
  # body.  This is a horrible hack, although it's debatable whether it was
  # better or worse when it was $self-&gt;{body} = shift @bits ... -- rjbs,
  # 2006-11-27
  $self-&gt;SUPER::body_set(shift @bits) if ($bits[0] || '') !~ /.*:.*/;

  my $bits = @bits;

  my @parts;
  for my $bit (@bits) {
    $bit =~ s/\A[\n\r]+//smg;
    $bit =~ s/(?&lt;!\x0d)$self-&gt;{mycrlf}\Z//sm;
    my $email = (ref $self)-&gt;new($bit, { encode_check =&gt; $self-&gt;encode_check });
    push @parts, $email;
  }

  $self-&gt;{parts} = \@parts;

  return @{ $self-&gt;{parts} };
}

sub force_decode_hook { 0 }
sub decode_hook       { return $_[1] }
sub content_type      { scalar shift-&gt;header("Content-type"); }

sub debug_structure {
  my ($self, $level) = @_;
  $level ||= 0;
  my $rv = " " x (5 * $level);
  $rv .= "+ " . ($self-&gt;content_type || '') . "\n";
  my @parts = $self-&gt;subparts;
  $rv .= $_-&gt;debug_structure($level + 1) for @parts;
  return $rv;
}

my %gcache;

sub filename {
  my ($self, $force) = @_;
  return $gcache{$self} if exists $gcache{$self};

  my $dis = $self-&gt;header("Content-Disposition") || '';
  my $attrs = parse_content_disposition($dis)-&gt;{attributes};
  my $name = $attrs-&gt;{filename}
    || $self-&gt;{ct}{attributes}{name};
  return $name if $name or !$force;
  return $gcache{$self} = $self-&gt;invent_filename(
    $self-&gt;{ct}-&gt;{type} . "/" . $self-&gt;{ct}-&gt;{subtype});
}

my $gname = 0;

sub invent_filename {
  my ($self, $ct) = @_;
  require MIME::Types;
  my $type = MIME::Types-&gt;new-&gt;type($ct);
  my $ext = $type &amp;&amp; (($type-&gt;extensions)[0]);
  $ext ||= "dat";
  return "attachment-$$-" . $gname++ . ".$ext";
}

sub default_header_class { 'Email::MIME::Header' }

sub header_str {
  my $self = shift;
  $self-&gt;header_obj-&gt;header_str(@_);
}

sub header_str_set {
  my $self = shift;
  $self-&gt;header_obj-&gt;header_str_set(@_);
}

sub header_str_pairs {
  my $self = shift;
  $self-&gt;header_obj-&gt;header_str_pairs(@_);
}

sub header_as_obj {
  my $self = shift;
  $self-&gt;header_obj-&gt;header_as_obj(@_);
}

#pod =method content_type_set
#pod
#pod   $email-&gt;content_type_set( 'text/html' );
#pod
#pod Change the content type. All C&lt;Content-Type&gt; header attributes
#pod will remain intact.
#pod
#pod =cut

sub content_type_set {
  my ($self, $ct) = @_;
  my $ct_header = parse_content_type($self-&gt;header('Content-Type'));
  @{$ct_header}{qw[type subtype]} = split m[/], $ct;
  $self-&gt;_compose_content_type($ct_header);
  $self-&gt;_reset_cids;
  return $ct;
}

#pod =method charset_set
#pod
#pod =method name_set
#pod
#pod =method format_set
#pod
#pod =method boundary_set
#pod
#pod   $email-&gt;charset_set( 'UTF-8' );
#pod   $email-&gt;name_set( 'some_filename.txt' );
#pod   $email-&gt;format_set( 'flowed' );
#pod   $email-&gt;boundary_set( undef ); # remove the boundary
#pod
#pod These four methods modify common C&lt;Content-Type&gt; attributes. If set to
#pod C&lt;undef&gt;, the attribute is removed. All other C&lt;Content-Type&gt; header
#pod information is preserved when modifying an attribute.
#pod
#pod =cut

BEGIN {
  foreach my $attr (qw[charset name format]) {
    my $code = sub {
      my ($self, $value) = @_;
      my $ct_header = parse_content_type($self-&gt;header('Content-Type'));
      if ($value) {
        $ct_header-&gt;{attributes}-&gt;{$attr} = $value;
      } else {
        delete $ct_header-&gt;{attributes}-&gt;{$attr};
      }
      $self-&gt;_compose_content_type($ct_header);
      return $value;
    };

    no strict 'refs';  ## no critic strict
    *{"$attr\_set"} = $code;
  }
}

sub boundary_set {
  my ($self, $value) = @_;
  my $ct_header = parse_content_type($self-&gt;header('Content-Type'));

  if ($value) {
    $ct_header-&gt;{attributes}-&gt;{boundary} = $value;
  } else {
    delete $ct_header-&gt;{attributes}-&gt;{boundary};
  }
  $self-&gt;_compose_content_type($ct_header);

  $self-&gt;parts_set([ $self-&gt;parts ]) if $self-&gt;parts &gt; 1;
}

sub content_type_attribute_set {
  my ($self, $key, $value) = @_;
  $key = lc $key;

  if ($CT_SETTER{$key}) {
    my $method = "$key\_set";
    return $self-&gt;$method($value);
  }

  my $ct_header = parse_content_type($self-&gt;header('Content-Type'));
  my $attrs = $ct_header-&gt;{attributes};

  for my $existing_key (keys %$attrs) {
    delete $attrs-&gt;{$existing_key} if lc $existing_key eq $key;
  }

  if ($value) {
    $ct_header-&gt;{attributes}-&gt;{$key} = $value;
  } else {
    delete $ct_header-&gt;{attributes}-&gt;{$key};
  }
  $self-&gt;_compose_content_type($ct_header);
}

#pod =method encode_check
#pod
#pod =method encode_check_set
#pod
#pod   $email-&gt;encode_check;
#pod   $email-&gt;encode_check_set(0);
#pod   $email-&gt;encode_check_set(Encode::FB_DEFAULT);
#pod
#pod Gets/sets the current C&lt;encode_check&gt; setting (default: I&lt;FB_CROAK&gt;).
#pod This is the parameter passed to L&lt;Encode/"decode"&gt; and L&lt;Encode/"encode"&gt;
#pod when C&lt;body_str()&gt;, C&lt;body_str_set()&gt;, and C&lt;create()&gt; are called.
#pod
#pod With the default setting, Email::MIME may crash if the claimed charset
#pod of a body does not match its contents (for example - utf8 data in a
#pod text/plain; charset=us-ascii message).
#pod
#pod With an C&lt;encode_check&gt; of 0, the unrecognized bytes will instead be
#pod replaced with the C&lt;REPLACEMENT CHARACTER&gt; (U+0FFFD), and may end up
#pod as either that or question marks (?).
#pod
#pod See L&lt;Encode/"Handling Malformed Data"&gt; for more information.
#pod
#pod =cut

sub encode_check {
  my ($self) = @_;

  return $self-&gt;{encode_check};
}

sub encode_check_set {
  my ($self, $val) = @_;

  return $self-&gt;{encode_check} = $val;
}

#pod =method encoding_set
#pod
#pod   $email-&gt;encoding_set( 'base64' );
#pod   $email-&gt;encoding_set( 'quoted-printable' );
#pod   $email-&gt;encoding_set( '8bit' );
#pod
#pod Convert the message body and alter the C&lt;Content-Transfer-Encoding&gt;
#pod header using this method. Your message body, the output of the C&lt;body()&gt;
#pod method, will remain the same. The raw body, output with the C&lt;body_raw()&gt;
#pod method, will be changed to reflect the new encoding.
#pod
#pod =cut

sub encoding_set {
  my ($self, $enc) = @_;
  $enc ||= '7bit';
  my $body = $self-&gt;body;
  $self-&gt;header_raw_set('Content-Transfer-Encoding' =&gt; $enc);
  $self-&gt;body_set($body);
}

#pod =method body_set
#pod
#pod   $email-&gt;body_set( $unencoded_body_string );
#pod
#pod This method will encode the new body you send using the encoding
#pod specified in the C&lt;Content-Transfer-Encoding&gt; header, then set
#pod the body to the new encoded body.
#pod
#pod This method overrides the default C&lt;body_set()&gt; method.
#pod
#pod =cut

sub body_set {
  my ($self, $body) = @_;
  my $body_ref;

  if (ref $body) {
    Carp::croak("provided body reference is not a scalar reference")
      unless reftype($body) eq 'SCALAR';
    $body_ref = $body;
  } else {
    $body_ref = \$body;
  }
  my $enc = $self-&gt;header('Content-Transfer-Encoding');

  # XXX: This is a disgusting hack and needs to be fixed, probably by a
  # clearer definition and reengineering of Simple construction.  The bug
  # this fixes is an indirect result of the previous behavior in which all
  # Simple subclasses were free to alter the guts of the Email::Simple
  # object. -- rjbs, 2007-07-16
  unless (((caller(1))[3] || '') eq 'Email::Simple::new') {
    $$body_ref = Email::MIME::Encodings::encode($enc, $$body_ref)
      unless !$enc || $enc =~ $NO_ENCODE_RE;
  }

  $self-&gt;{body_raw} = $$body_ref;
  $self-&gt;SUPER::body_set($body_ref);
}

#pod =method body_str_set
#pod
#pod   $email-&gt;body_str_set($unicode_str);
#pod
#pod This method behaves like C&lt;body_set&gt;, but assumes that the given value is a
#pod Unicode string that should be encoded into the message's charset
#pod before being set.
#pod
#pod The charset must already be set, either manually (via the C&lt;attributes&gt;
#pod argument to C&lt;create&gt; or C&lt;charset_set&gt;) or through the C&lt;Content-Type&gt; of a
#pod parsed message.  If the charset can't be determined, an exception is thrown.
#pod
#pod =cut

sub body_str_set {
  my ($self, $body_str) = @_;

  my $ct = parse_content_type($self-&gt;content_type);
  Carp::confess("body_str was given, but no charset is defined")
    unless my $charset = $ct-&gt;{attributes}{charset};

  my $body_octets = Encode::encode($charset, $body_str, $self-&gt;encode_check);
  $self-&gt;body_set($body_octets);
}

#pod =method disposition_set
#pod
#pod   $email-&gt;disposition_set( 'attachment' );
#pod
#pod Alter the C&lt;Content-Disposition&gt; of a message. All header attributes
#pod will remain intact.
#pod
#pod =cut

sub disposition_set {
  my ($self, $dis) = @_;
  $dis ||= 'inline';
  my $dis_header = $self-&gt;header('Content-Disposition');
  $dis_header
    ? ($dis_header =~ s/^([^;]+)/$dis/)
    : ($dis_header = $dis);
  $self-&gt;header_raw_set('Content-Disposition' =&gt; $dis_header);
}

#pod =method filename_set
#pod
#pod   $email-&gt;filename_set( 'boo.pdf' );
#pod
#pod Sets the filename attribute in the C&lt;Content-Disposition&gt; header. All other
#pod header information is preserved when setting this attribute.
#pod
#pod =cut

sub filename_set {
  my ($self, $filename) = @_;
  my $dis_header = $self-&gt;header('Content-Disposition');
  my ($disposition, $attrs);
  if ($dis_header) {
    my $struct = parse_content_disposition($dis_header);
    $disposition = $struct-&gt;{type};
    $attrs = $struct-&gt;{attributes};
  }
  $filename ? $attrs-&gt;{filename} = $filename : delete $attrs-&gt;{filename};
  $disposition ||= 'inline';

  my $dis = $disposition;
  while (my ($attr, $val) = each %{$attrs}) {
    $dis .= qq[; $attr="$val"];
  }

  $self-&gt;header_raw_set('Content-Disposition' =&gt; $dis);
}

#pod =method parts_set
#pod
#pod   $email-&gt;parts_set( \@new_parts );
#pod
#pod Replaces the parts for an object. Accepts a reference to a list of
#pod C&lt;Email::MIME&gt; objects, representing the new parts. If this message was
#pod originally a single part, the C&lt;Content-Type&gt; header will be changed to
#pod C&lt;multipart/mixed&gt;, and given a new boundary attribute.
#pod
#pod =cut

sub parts_set {
  my ($self, $parts) = @_;
  my $body = q{};

  my $ct_header = parse_content_type($self-&gt;header('Content-Type'));

  if (@{$parts} &gt; 1 or $ct_header-&gt;{type} eq 'multipart') {

    # setup multipart
    $ct_header-&gt;{attributes}-&gt;{boundary} ||= Email::MessageID-&gt;new-&gt;user;
    my $bound = $ct_header-&gt;{attributes}-&gt;{boundary};
    foreach my $part (@{$parts}) {
      $body .= "$self-&gt;{mycrlf}--$bound$self-&gt;{mycrlf}";
      $body .= $part-&gt;as_string;
    }
    $body .= "$self-&gt;{mycrlf}--$bound--$self-&gt;{mycrlf}";

    unless (grep { $ct_header-&gt;{type} eq $_ } qw[multipart message]) {
      if (scalar $self-&gt;header('Content-Type')) {
        Carp::carp("replacing non-multipart type ($ct_header-&gt;{type}/$ct_header-&gt;{subtype}) with multipart/mixed");
      }
      @{$ct_header}{qw[type subtype]} = qw[multipart mixed];
    }

    $self-&gt;encoding_set('7bit');
    delete $ct_header-&gt;{attributes}{charset};
  } elsif (@$parts == 1) {  # setup singlepart
    $body .= $parts-&gt;[0]-&gt;body;

    my $from_ct = parse_content_type($parts-&gt;[0]-&gt;header('Content-Type'));
    @{$ct_header}{qw[type subtype]} = @{ $from_ct }{qw[type subtype]};

    $ct_header-&gt;{attributes}{charset} = $from_ct-&gt;{attributes}{charset};

    $self-&gt;encoding_set($parts-&gt;[0]-&gt;header('Content-Transfer-Encoding'));
    delete $ct_header-&gt;{attributes}-&gt;{boundary};
  }

  $self-&gt;_compose_content_type($ct_header);
  $self-&gt;body_set($body);
  $self-&gt;fill_parts;
  $self-&gt;_reset_cids;
}

#pod =method parts_add
#pod
#pod   $email-&gt;parts_add( \@more_parts );
#pod
#pod Adds MIME parts onto the current MIME part. This is a simple extension
#pod of C&lt;parts_set&gt; to make our lives easier. It accepts an array reference
#pod of additional parts.
#pod
#pod =cut

sub parts_add {
  my ($self, $parts) = @_;
  $self-&gt;parts_set([ $self-&gt;parts, @{$parts}, ]);
}

#pod =method walk_parts
#pod
#pod   $email-&gt;walk_parts(sub {
#pod       my ($part) = @_;
#pod       return if $part-&gt;subparts; # multipart
#pod
#pod       if ( $part-&gt;content_type =~ m[text/html]i ) {
#pod           my $body = $part-&gt;body;
#pod           $body =~ s/&lt;link [^&gt;]+&gt;//; # simple filter example
#pod           $part-&gt;body_set( $body );
#pod       }
#pod   });
#pod
#pod Walks through all the MIME parts in a message and applies a callback to
#pod each. Accepts a code reference as its only argument. The code reference
#pod will be passed a single argument, the current MIME part within the
#pod top-level MIME object. All changes will be applied in place.
#pod
#pod =cut

sub walk_parts {
  my ($self, $callback) = @_;

  my %changed;

  my $walk_weak;
  my $walk = sub {
    my ($part) = @_;
    $callback-&gt;($part);

    if (my @orig_subparts = $part-&gt;subparts) {
      my $differ;
      my @subparts;

      for my $part (@orig_subparts) {
        my $str = $part-&gt;as_string;
        next unless my $new = $walk_weak-&gt;($part);
        $differ = 1 if $str ne $new-&gt;as_string;
        push @subparts, $new;
      }

      $differ
        ||=  (@subparts != @orig_subparts)
        ||   (grep { $subparts[$_] != $orig_subparts[$_] } (0 .. $#subparts))
        ||   (grep { $changed{ 0+$subparts[$_] } } (0 .. $#subparts));

      if ($differ) {
        $part-&gt;parts_set(\@subparts);
        $changed{ 0+$part }++;
      }
    }

    return $part;
  };

  $walk_weak = $walk;
  weaken $walk_weak;

  my $rv = $walk-&gt;($self);

  undef $walk;

  return $rv;
}

sub _compose_content_type {
  my ($self, $ct_header) = @_;
  my $ct = join q{/}, @{$ct_header}{qw[type subtype]};
  for my $attr (sort keys %{ $ct_header-&gt;{attributes} }) {
    next unless defined (my $value = $ct_header-&gt;{attributes}{$attr});
    $ct .= qq[; $attr="$value"];
  }
  $self-&gt;header_raw_set('Content-Type' =&gt; $ct);
  $self-&gt;{ct} = $ct_header;
}

sub _get_cid {
  Email::MessageID-&gt;new-&gt;address;
}

sub _reset_cids {
  my ($self) = @_;

  my $ct_header = parse_content_type($self-&gt;header('Content-Type'));

  if ($self-&gt;parts &gt; 1) {
    if ($ct_header-&gt;{subtype} eq 'alternative') {
      my %cids;
      for my $part ($self-&gt;parts) {
        my $cid
          = defined $part-&gt;header('Content-ID')
          ? $part-&gt;header('Content-ID')
          : q{};
        $cids{$cid}++;
      }
      return if keys(%cids) == 1;

      my $cid = $self-&gt;_get_cid;
      $_-&gt;header_raw_set('Content-ID' =&gt; "&lt;$cid&gt;") for $self-&gt;parts;
    } else {
      foreach ($self-&gt;parts) {
        my $cid = $self-&gt;_get_cid;
        $_-&gt;header_raw_set('Content-ID' =&gt; "&lt;$cid&gt;")
          unless $_-&gt;header('Content-ID');
      }
    }
  }
}

1;

=pod

=encoding UTF-8

=head1 NAME

Email::MIME - easy MIME message handling

=head1 VERSION

version 1.946

=head1 SYNOPSIS

B&lt;Wait!&gt;  Before you read this, maybe you just need L&lt;Email::Stuffer&gt;, which is
a much easier-to-use tool for building simple email messages that might have
attachments or both plain text and HTML.  If that doesn't do it for you, then
by all means keep reading.

  use Email::MIME;
  my $parsed = Email::MIME-&gt;new($message);

  my @parts = $parsed-&gt;parts; # These will be Email::MIME objects, too.
  my $decoded = $parsed-&gt;body;
  my $non_decoded = $parsed-&gt;body_raw;

  my $content_type = $parsed-&gt;content_type;

...or...

  use Email::MIME;
  use IO::All;

  # multipart message
  my @parts = (
      Email::MIME-&gt;create(
          attributes =&gt; {
              filename     =&gt; "report.pdf",
              content_type =&gt; "application/pdf",
              encoding     =&gt; "quoted-printable",
              name         =&gt; "2004-financials.pdf",
          },
          body =&gt; io( "2004-financials.pdf" )-&gt;binary-&gt;all,
      ),
      Email::MIME-&gt;create(
          attributes =&gt; {
              content_type =&gt; "text/plain",
              disposition  =&gt; "attachment",
              charset      =&gt; "US-ASCII",
          },
          body_str =&gt; "Hello there!",
      ),
  );

  my $email = Email::MIME-&gt;create(
      header_str =&gt; [
          From =&gt; 'casey@geeknest.com',
          To =&gt; [ 'user1@host.com', 'Name &lt;user2@host.com&gt;' ],
          Cc =&gt; Email::Address::XS-&gt;new("Display Name \N{U+1F600}", 'user@example.com'),
      ],
      parts      =&gt; [ @parts ],
  );

  # nesting parts
  $email-&gt;parts_set(
      [
        $email-&gt;parts,
        Email::MIME-&gt;create( parts =&gt; [ @parts ] ),
      ],
  );

  # standard modifications
  $email-&gt;header_str_set( 'X-PoweredBy' =&gt; 'RT v3.0'      );
  $email-&gt;header_str_set( To            =&gt; rcpts()        );
  $email-&gt;header_str_set( Cc            =&gt; aux_rcpts()    );
  $email-&gt;header_str_set( Bcc           =&gt; sekrit_rcpts() );

  # more advanced
  $_-&gt;encoding_set( 'base64' ) for $email-&gt;parts;

  # Quick multipart creation
  my $email = Email::MIME-&gt;create(
      header_str =&gt; [
          From =&gt; 'my@address',
          To   =&gt; 'your@address',
      ],
      parts =&gt; [
          q[This is part one],
          q[This is part two],
          q[These could be binary too],
      ],
  );

  print $email-&gt;as_string;

=head1 DESCRIPTION

This is an extension of the L&lt;Email::Simple&gt; module, to handle MIME
encoded messages. It takes a message as a string, splits it up into its
constituent parts, and allows you access to various parts of the
message. Headers are decoded from MIME encoding.

=head1 METHODS

Please see L&lt;Email::Simple&gt; for the base set of methods. It won't take
very long. Added to that, you have:

=head2 create

  my $single = Email::MIME-&gt;create(
    header_str =&gt; [ ... ],
    body_str   =&gt; '...',
    attributes =&gt; { ... },
  );

  my $multi = Email::MIME-&gt;create(
    header_str =&gt; [ ... ],
    parts      =&gt; [ ... ],
    attributes =&gt; { ... },
  );

This method creates a new MIME part. The C&lt;header_str&gt; parameter is a list of
headers pairs to include in the message. The value for each pair is expected to
be a text string that will be MIME-encoded as needed.  Alternatively it can be
an object with C&lt;as_mime_string&gt; method which implements conversion of that
object to MIME-encoded string.  That object method is called with two named
input parameters: C&lt;charset&gt; and C&lt;header_name_length&gt;.  It should return
MIME-encoded representation of the object.  As of 2017-07-25, the
header-value-as-object code is very young, and may yet change.

In case header name is registered in C&lt;%Email::MIME::Header::header_to_class_map&gt;
hash then registered class is used for conversion from Unicode string to 8bit
MIME encoding.  Value can be either string or array reference to strings.
Object is constructed via method C&lt;from_string&gt; with string value (or values
in case of array reference) and converted to MIME-encoded string via
C&lt;as_mime_string&gt; method.

A similar C&lt;header&gt; parameter can be provided in addition to or instead of
C&lt;header_str&gt;.  Its values will be used verbatim.

C&lt;attributes&gt; is a hash of MIME attributes to assign to the part, and may
override portions of the header set in the C&lt;header&gt; parameter. The hash keys
correspond directly to methods or modifying a message from
C&lt;Email::MIME::Modifier&gt;. The allowed keys are: content_type, charset, name,
format, boundary, encoding, disposition, and filename. They will be mapped to
C&lt;"$attr\_set"&gt; for message modification.

The C&lt;parts&gt; parameter is a list reference containing C&lt;Email::MIME&gt;
objects. Elements of the C&lt;parts&gt; list can also be a non-reference
string of data. In that case, an C&lt;Email::MIME&gt; object will be created
for you. Simple checks will determine if the part is binary or not, and
all parts created in this fashion are encoded with C&lt;base64&gt;, just in case.

If C&lt;body&gt; is given instead of C&lt;parts&gt;, it specifies the body to be used for a
flat (subpart-less) MIME message.  It is assumed to be a sequence of octets.

If C&lt;body_str&gt; is given instead of C&lt;body&gt; or C&lt;parts&gt;, it is assumed to be a
character string to be used as the body.  If you provide a C&lt;body_str&gt;
parameter, you B&lt;must&gt; provide C&lt;charset&gt; and C&lt;encoding&gt; attributes.

=head2 content_type_set

  $email-&gt;content_type_set( 'text/html' );

Change the content type. All C&lt;Content-Type&gt; header attributes
will remain intact.

=head2 charset_set

=head2 name_set

=head2 format_set

=head2 boundary_set

  $email-&gt;charset_set( 'UTF-8' );
  $email-&gt;name_set( 'some_filename.txt' );
  $email-&gt;format_set( 'flowed' );
  $email-&gt;boundary_set( undef ); # remove the boundary

These four methods modify common C&lt;Content-Type&gt; attributes. If set to
C&lt;undef&gt;, the attribute is removed. All other C&lt;Content-Type&gt; header
information is preserved when modifying an attribute.

=head2 encode_check

=head2 encode_check_set

  $email-&gt;encode_check;
  $email-&gt;encode_check_set(0);
  $email-&gt;encode_check_set(Encode::FB_DEFAULT);

Gets/sets the current C&lt;encode_check&gt; setting (default: I&lt;FB_CROAK&gt;).
This is the parameter passed to L&lt;Encode/"decode"&gt; and L&lt;Encode/"encode"&gt;
when C&lt;body_str()&gt;, C&lt;body_str_set()&gt;, and C&lt;create()&gt; are called.

With the default setting, Email::MIME may crash if the claimed charset
of a body does not match its contents (for example - utf8 data in a
text/plain; charset=us-ascii message).

With an C&lt;encode_check&gt; of 0, the unrecognized bytes will instead be
replaced with the C&lt;REPLACEMENT CHARACTER&gt; (U+0FFFD), and may end up
as either that or question marks (?).

See L&lt;Encode/"Handling Malformed Data"&gt; for more information.

=head2 encoding_set

  $email-&gt;encoding_set( 'base64' );
  $email-&gt;encoding_set( 'quoted-printable' );
  $email-&gt;encoding_set( '8bit' );

Convert the message body and alter the C&lt;Content-Transfer-Encoding&gt;
header using this method. Your message body, the output of the C&lt;body()&gt;
method, will remain the same. The raw body, output with the C&lt;body_raw()&gt;
method, will be changed to reflect the new encoding.

=head2 body_set

  $email-&gt;body_set( $unencoded_body_string );

This method will encode the new body you send using the encoding
specified in the C&lt;Content-Transfer-Encoding&gt; header, then set
the body to the new encoded body.

This method overrides the default C&lt;body_set()&gt; method.

=head2 body_str_set

  $email-&gt;body_str_set($unicode_str);

This method behaves like C&lt;body_set&gt;, but assumes that the given value is a
Unicode string that should be encoded into the message's charset
before being set.

The charset must already be set, either manually (via the C&lt;attributes&gt;
argument to C&lt;create&gt; or C&lt;charset_set&gt;) or through the C&lt;Content-Type&gt; of a
parsed message.  If the charset can't be determined, an exception is thrown.

=head2 disposition_set

  $email-&gt;disposition_set( 'attachment' );

Alter the C&lt;Content-Disposition&gt; of a message. All header attributes
will remain intact.

=head2 filename_set

  $email-&gt;filename_set( 'boo.pdf' );

Sets the filename attribute in the C&lt;Content-Disposition&gt; header. All other
header information is preserved when setting this attribute.

=head2 parts_set

  $email-&gt;parts_set( \@new_parts );

Replaces the parts for an object. Accepts a reference to a list of
C&lt;Email::MIME&gt; objects, representing the new parts. If this message was
originally a single part, the C&lt;Content-Type&gt; header will be changed to
C&lt;multipart/mixed&gt;, and given a new boundary attribute.

=head2 parts_add

  $email-&gt;parts_add( \@more_parts );

Adds MIME parts onto the current MIME part. This is a simple extension
of C&lt;parts_set&gt; to make our lives easier. It accepts an array reference
of additional parts.

=head2 walk_parts

  $email-&gt;walk_parts(sub {
      my ($part) = @_;
      return if $part-&gt;subparts; # multipart

      if ( $part-&gt;content_type =~ m[text/html]i ) {
          my $body = $part-&gt;body;
          $body =~ s/&lt;link [^&gt;]+&gt;//; # simple filter example
          $part-&gt;body_set( $body );
      }
  });

Walks through all the MIME parts in a message and applies a callback to
each. Accepts a code reference as its only argument. The code reference
will be passed a single argument, the current MIME part within the
top-level MIME object. All changes will be applied in place.

=head2 header

B&lt;Achtung!&gt;  Beware this method!  In Email::MIME, it means the same as
C&lt;header_str&gt;, but on an Email::Simple object, it means C&lt;header_raw&gt;.  Unless
you always know what kind of object you have, you could get one of two
significantly different behaviors.

Try to use either C&lt;header_str&gt; or C&lt;header_raw&gt; as appropriate.

=head2 header_str_set

  $email-&gt;header_str_set($header_name =&gt; @value_strings);

This behaves like C&lt;header_raw_set&gt;, but expects Unicode (character) strings as
the values to set, rather than pre-encoded byte strings.  It will encode them
as MIME encoded-words if they contain any control or 8-bit characters.

Alternatively, values can be objects with C&lt;as_mime_string&gt; method.  Same as in
method C&lt;create&gt;.

=head2 header_str_pairs

  my @pairs = $email-&gt;header_str_pairs;

This method behaves like C&lt;header_raw_pairs&gt;, returning a list of field
name/value pairs, but the values have been decoded to character strings, when
possible.

=head2 header_as_obj

  my $first_obj = $email-&gt;header_as_obj($field);
  my $nth_obj   = $email-&gt;header_as_obj($field, $index);
  my @all_objs  = $email-&gt;header_as_obj($field);

  my $nth_obj_of_class  = $email-&gt;header_as_obj($field, $index, $class);
  my @all_objs_of_class = $email-&gt;header_as_obj($field, undef, $class);

This method returns an object representation of the header value.  It instances
new object via method C&lt;from_mime_string&gt; of specified class.  Input argument
for that class method is list of the raw MIME-encoded values.  If class argument
is not specified then class name is taken from the hash
C&lt;%Email::MIME::Header::header_to_class_map&gt; via key field.  Use class method
C&lt;&lt; Email::MIME::Header-&gt;set_class_for_header($class, $field) &gt;&gt; for adding new
mapping.

=head2 parts

This returns a list of C&lt;Email::MIME&gt; objects reflecting the parts of the
message. If it's a single-part message, you get the original object back.

In scalar context, this method returns the number of parts.

This is a stupid method.  Don't use it.

=head2 subparts

This returns a list of C&lt;Email::MIME&gt; objects reflecting the parts of the
message.  If it's a single-part message, this method returns an empty list.

In scalar context, this method returns the number of subparts.

=head2 body

This decodes and returns the body of the object I&lt;as a byte string&gt;. For
top-level objects in multi-part messages, this is highly likely to be something
like "This is a multi-part message in MIME format."

=head2 body_str

This decodes both the Content-Transfer-Encoding layer of the body (like the
C&lt;body&gt; method) as well as the charset encoding of the body (unlike the C&lt;body&gt;
method), returning a Unicode string.

If the charset is known, it is used.  If there is no charset but the content
type is either C&lt;text/plain&gt; or C&lt;text/html&gt;, us-ascii is assumed.  Otherwise,
an exception is thrown.

=head2 body_raw

This returns the body of the object, but doesn't decode the transfer encoding.

=head2 decode_hook

This method is called before the L&lt;Email::MIME::Encodings&gt; C&lt;decode&gt; method, to
decode the body of non-binary messages (or binary messages, if the
C&lt;force_decode_hook&gt; method returns true).  By default, this method does
nothing, but subclasses may define behavior.

This method could be used to implement the decryption of content in secure
email, for example.

=head2 content_type

This is a shortcut for access to the content type header.

=head2 filename

This provides the suggested filename for the attachment part. Normally
it will return the filename from the headers, but if C&lt;filename&gt; is
passed a true parameter, it will generate an appropriate "stable"
filename if one is not found in the MIME headers.

=head2 invent_filename

  my $filename = Email::MIME-&gt;invent_filename($content_type);

This routine is used by C&lt;filename&gt; to generate filenames for attached files.
It will attempt to choose a reasonable extension, falling back to F&lt;dat&gt;.

=head2 debug_structure

  my $description = $email-&gt;debug_structure;

This method returns a string that describes the structure of the MIME entity.
For example:

  + multipart/alternative; boundary="=_NextPart_2"; charset="BIG-5"
    + text/plain
    + text/html

=head1 TODO

All of the Email::MIME-specific guts should move to a single entry on the
object's guts.  This will require changes to both Email::MIME and
L&lt;Email::MIME::Modifier&gt;, sadly.

=head1 SEE ALSO

L&lt;Email::Simple&gt;, L&lt;Email::MIME::Modifier&gt;, L&lt;Email::MIME::Creator&gt;.

=head1 THANKS

This module was generously sponsored by Best Practical
(http://www.bestpractical.com/), Pete Sergeant, and Pobox.com.

=head1 AUTHORS

=over 4

=item *

Ricardo SIGNES &lt;rjbs@cpan.org&gt;

=item *

Casey West &lt;casey@geeknest.com&gt;

=item *

Simon Cozens &lt;simon@cpan.org&gt;

=back

=head1 CONTRIBUTORS

=for stopwords Alex Vandiver Arthur Axel 'fREW' Schmidt Brian Cassidy Dan Book David Steinbrunner Dotan Dimet Geraint Edwards Jesse Luehrs Kurt Anderson Lance A. Brown Matthew Horsfall (alh) memememomo Michael McClimon Pali Shawn Sorichetti Tomohiro Hosaka

=over 4

=item *

Alex Vandiver &lt;alexmv@mit.edu&gt;

=item *

Arthur Axel 'fREW' Schmidt &lt;frioux@gmail.com&gt;

=item *

Brian Cassidy &lt;bricas@cpan.org&gt;

=item *

Dan Book &lt;grinnz@gmail.com&gt;

=item *

David Steinbrunner &lt;dsteinbrunner@pobox.com&gt;

=item *

Dotan Dimet &lt;dotan@corky.net&gt;

=item *

Geraint Edwards &lt;gedge-oss@yadn.org&gt;

=item *

Jesse Luehrs &lt;doy@tozt.net&gt;

=item *

Kurt Anderson &lt;kboth@drkurt.com&gt;

=item *

Lance A. Brown &lt;lance@bearcircle.net&gt;

=item *

Matthew Horsfall (alh) &lt;wolfsage@gmail.com&gt;

=item *

memememomo &lt;memememomo@gmail.com&gt;

=item *

Michael McClimon &lt;michael@mcclimon.org&gt;

=item *

Pali &lt;pali@cpan.org&gt;

=item *

Shawn Sorichetti &lt;ssoriche@coloredblocks.com&gt;

=item *

Tomohiro Hosaka &lt;bokutin@bokut.in&gt;

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2004 by Simon Cozens and Casey West.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

__END__

#pod =method header
#pod
#pod B&lt;Achtung!&gt;  Beware this method!  In Email::MIME, it means the same as
#pod C&lt;header_str&gt;, but on an Email::Simple object, it means C&lt;header_raw&gt;.  Unless
#pod you always know what kind of object you have, you could get one of two
#pod significantly different behaviors.
#pod
#pod Try to use either C&lt;header_str&gt; or C&lt;header_raw&gt; as appropriate.
#pod
#pod =method header_str_set
#pod
#pod   $email-&gt;header_str_set($header_name =&gt; @value_strings);
#pod
#pod This behaves like C&lt;header_raw_set&gt;, but expects Unicode (character) strings as
#pod the values to set, rather than pre-encoded byte strings.  It will encode them
#pod as MIME encoded-words if they contain any control or 8-bit characters.
#pod
#pod Alternatively, values can be objects with C&lt;as_mime_string&gt; method.  Same as in
#pod method C&lt;create&gt;.
#pod
#pod =method header_str_pairs
#pod
#pod   my @pairs = $email-&gt;header_str_pairs;
#pod
#pod This method behaves like C&lt;header_raw_pairs&gt;, returning a list of field
#pod name/value pairs, but the values have been decoded to character strings, when
#pod possible.
#pod
#pod =method header_as_obj
#pod
#pod   my $first_obj = $email-&gt;header_as_obj($field);
#pod   my $nth_obj   = $email-&gt;header_as_obj($field, $index);
#pod   my @all_objs  = $email-&gt;header_as_obj($field);
#pod
#pod   my $nth_obj_of_class  = $email-&gt;header_as_obj($field, $index, $class);
#pod   my @all_objs_of_class = $email-&gt;header_as_obj($field, undef, $class);
#pod
#pod This method returns an object representation of the header value.  It instances
#pod new object via method C&lt;from_mime_string&gt; of specified class.  Input argument
#pod for that class method is list of the raw MIME-encoded values.  If class argument
#pod is not specified then class name is taken from the hash
#pod C&lt;%Email::MIME::Header::header_to_class_map&gt; via key field.  Use class method
#pod C&lt;&lt; Email::MIME::Header-&gt;set_class_for_header($class, $field) &gt;&gt; for adding new
#pod mapping.
#pod
#pod =method parts
#pod
#pod This returns a list of C&lt;Email::MIME&gt; objects reflecting the parts of the
#pod message. If it's a single-part message, you get the original object back.
#pod
#pod In scalar context, this method returns the number of parts.
#pod
#pod This is a stupid method.  Don't use it.
#pod
#pod =method subparts
#pod
#pod This returns a list of C&lt;Email::MIME&gt; objects reflecting the parts of the
#pod message.  If it's a single-part message, this method returns an empty list.
#pod
#pod In scalar context, this method returns the number of subparts.
#pod
#pod =method body
#pod
#pod This decodes and returns the body of the object I&lt;as a byte string&gt;. For
#pod top-level objects in multi-part messages, this is highly likely to be something
#pod like "This is a multi-part message in MIME format."
#pod
#pod =method body_str
#pod
#pod This decodes both the Content-Transfer-Encoding layer of the body (like the
#pod C&lt;body&gt; method) as well as the charset encoding of the body (unlike the C&lt;body&gt;
#pod method), returning a Unicode string.
#pod
#pod If the charset is known, it is used.  If there is no charset but the content
#pod type is either C&lt;text/plain&gt; or C&lt;text/html&gt;, us-ascii is assumed.  Otherwise,
#pod an exception is thrown.
#pod
#pod =method body_raw
#pod
#pod This returns the body of the object, but doesn't decode the transfer encoding.
#pod
#pod =method decode_hook
#pod
#pod This method is called before the L&lt;Email::MIME::Encodings&gt; C&lt;decode&gt; method, to
#pod decode the body of non-binary messages (or binary messages, if the
#pod C&lt;force_decode_hook&gt; method returns true).  By default, this method does
#pod nothing, but subclasses may define behavior.
#pod
#pod This method could be used to implement the decryption of content in secure
#pod email, for example.
#pod
#pod =method content_type
#pod
#pod This is a shortcut for access to the content type header.
#pod
#pod =method filename
#pod
#pod This provides the suggested filename for the attachment part. Normally
#pod it will return the filename from the headers, but if C&lt;filename&gt; is
#pod passed a true parameter, it will generate an appropriate "stable"
#pod filename if one is not found in the MIME headers.
#pod
#pod =method invent_filename
#pod
#pod   my $filename = Email::MIME-&gt;invent_filename($content_type);
#pod
#pod This routine is used by C&lt;filename&gt; to generate filenames for attached files.
#pod It will attempt to choose a reasonable extension, falling back to F&lt;dat&gt;.
#pod
#pod =method debug_structure
#pod
#pod   my $description = $email-&gt;debug_structure;
#pod
#pod This method returns a string that describes the structure of the MIME entity.
#pod For example:
#pod
#pod   + multipart/alternative; boundary="=_NextPart_2"; charset="BIG-5"
#pod     + text/plain
#pod     + text/html
#pod
#pod =head1 TODO
#pod
#pod All of the Email::MIME-specific guts should move to a single entry on the
#pod object's guts.  This will require changes to both Email::MIME and
#pod L&lt;Email::MIME::Modifier&gt;, sadly.
#pod
#pod =head1 SEE ALSO
#pod
#pod L&lt;Email::Simple&gt;, L&lt;Email::MIME::Modifier&gt;, L&lt;Email::MIME::Creator&gt;.
#pod
#pod =head1 THANKS
#pod
#pod This module was generously sponsored by Best Practical
#pod (http://www.bestpractical.com/), Pete Sergeant, and Pobox.com.
#pod
#pod =cut
</pre></body></html>