使用Email :: MIME和multipart /与子部分

时间:2017-03-16 01:14:32

标签: perl email mime-types

我是一名perl新手并且一直在使用Email :: MIME来弄清楚如何正确解析多部分电子邮件。我刚刚发现另一种组合,我目前的努力无法正确阅读。

     Content-Type: multipart/mixed; boundary="===============1811908679642194059=="
 MIME-Version: 1.0

 This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
 --===============1811908679642194059==
 Content-Type: multipart/signed; micalg=pgp-sha256;
  protocol="application/pgp-signature";
  boundary="lGJM242FL2E9Wh4auTNwQRWOeFI0Wj9mB"

 This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
 --lGJM242FL2E9Wh4auTNwQRWOeFI0Wj9mB
 Content-Type: multipart/alternative;
  boundary="------------CC2F0C038668F58F6EDEA0D2"

 This is a multi-part message in MIME format.
 --------------CC2F0C038668F58F6EDEA0D2
 Content-Type: text/plain; charset=windows-1252
 Content-Transfer-Encoding: quoted-printable

 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

text / plain部分是我想要的部分,但是阅读" text"组件只是给了我"这是一个多部分......"这就行了。这是我开发的用于阅读具有类似子部分的其他电子邮件的代码,但它没有正确解释此部分。

它看起来与" body"作为Email :: MIME的一部分:

 This decodes and returns the body of the object as a byte string. 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."

在Email :: MIME中正确阅读此内容类型的正确功能是什么?

如何正确识别此电子邮件中的内容类型?它是" multipart / mixed"或" text / plain"或" multipart / alternative"?

我是否想在这里使用subparts方法?

 my @mailData;
 my $msg = Email::MIME->new($buf);
 foreach my $part ( $msg->subparts ) {
    foreach my $sub_part ($part->subparts) {
         print $sub_part->content_type;
        if ($sub_part->content_type =~ m!text!) {
            @mailData = split( '\n', $sub_part->body);
         }
    }
 }

上面的代码只打印"这是一个多部分的消息......"在@mailData数组中。

1 个答案:

答案 0 :(得分:2)

我花了最近几天使用Email :: MIME,MIME :: Parser和MIME :: Entity来自动处理大量电子邮件。我发现编写同一封电子邮件的标准方法很少,比我想象的要困难得多。

这是处理电子邮件标题和正文的非常可靠的方法。非常感谢所有在此过程中提供帮助的人。

 #!/usr/bin/perl -w

 use strict;
 use MIME::Parser;
 use MIME::Entity;
 use Email::MIME;

 # Read the email from STDIN
 my $buf;
 while(<STDIN> ){
         $buf .= $_;
 }

 # This creates msg-NNNN-N.txt and signature-N.asc files
 # and I don't know why. Related to output_to_core?
 my $parser = MIME::Parser->new;
 $parser->extract_uuencode(1);
 $parser->extract_nested_messages(1);
 $parser->output_to_core(0);

 # For reading headers
 my $entity = $parser->parse_data($buf);

 # For reading the body (of an mbox)
 my $msg = Email::MIME->new($buf);

 # Use MIME::Entity to read various headers. 
 my $subject = $entity->head->get('Subject');
 my $from = $entity->head->get('From');
 my $AdvDate = $entity->head->get('Date');
 $AdvDate =~ s/\n//g; $subject =~ s/\n//g; $from =~ s/\n//g;

 print "Subject: $subject\n";
 print "From: $from\n";
 print "Date: $AdvDate\n";

 my @mailData;

  # walk through all the different attachments. Stop at the first one that matches and
  # read its contents into mailData. The first one typically appeared to be the primary one.
  $msg->walk_parts(sub {
      my ($part) = @_;
      #warn($part->content_type . ": " . $part->subparts);
      if (($part->content_type =~ /text\/plain; charset=\"?utf-8\"?/i) && !@mailData) {
         #print $part->body;
         @mailData = split( '\n', $part->body);
      }
      elsif (($part->content_type =~ /text\/plain; charset=\"?us-ascii\"?/i) && !@mailData) {
         #print $part->body;
         @mailData = split( '\n', $part->body);
      }
      elsif (($part->content_type =~ /text\/plain; charset=\"?windows-1252\"?/i) && !@mailData) {
         #print $part->body;
         @mailData = split( '\n', $part->body);
      }
      elsif (($part->content_type =~ /text\/plain; charset=\"?iso-8859-1\"?/i) && !@mailData) {
         #print $part->body;
         @mailData = split( '\n', $part->body);
      }
  });


 # manipulate the body of the message stored in mailData
 foreach my $line (@mailData) {
        print "$line\n";
 }