【问题标题】:How can I strip HTML and attachments from emails?如何从电子邮件中删除 HTML 和附件?
【发布时间】:2010-09-27 03:14:48
【问题描述】:

我正在使用下面的程序对电子邮件进行排序并最终打印出来。某些邮件可能包含不利于打印的附件或 HTML 代码。有没有一种简单的方法可以从邮件中去除附件和 HTML,但不去除 HTML 格式的文本?

#!/usr/bin/perl
use warnings;
use strict;
use Mail::Box::Manager;

open (MYFILE, '>>data.txt');
binmode(MYFILE, ':encoding(UTF-8)');


my $file = shift || $ENV{MAIL};
my $mgr = Mail::Box::Manager->new(
    access          => 'r',
);

my $folder = $mgr->open( folder => $file )
or die "$file: Unable to open: $!\n";

for my $msg ( sort { $a->timestamp <=> $b->timestamp } $folder->messages)
{
    my $to          = join( ', ', map { $_->format } $msg->to );
    my $from        = join( ', ', map { $_->format } $msg->from );
    my $date        = localtime( $msg->timestamp );
    my $subject     = $msg->subject;
    my $body        = $msg->decoded->string;

    # Strip all quoted text
    $body =~ s/^>.*$//msg;

    print MYFILE <<"";
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

}

【问题讨论】:

    标签: html perl email attachment


    【解决方案1】:

    Mail::Message::isMultipart 会告诉你给定的邮件是否有任何附件。 Mail::Message::parts 会给你一个邮件部分的列表。

    因此:

    if ( $msg->isMultipart ) {
        foreach my $part ( $msg->parts ) {
            if ( $part->contentType eq 'text/html' ) {
               # deal with html here.
            }
            elsif ( $part->contentType eq 'text/plain' ) {
               # deal with text here.
            }
            else {
               # well?
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      常见问题解答 #9(或perldoc -q html 中的第一项)中解释了剥离 HTML 方面。简单来说,相关模块是 HTML::Parser 和 HTML::FormatText。

      至于附件,带有附件的电子邮件以 MIME 形式发送。从this example,您可以看到格式非常简单,您可以相当容易地提出解决方案,或者检查MIME modules at CPAN

      【讨论】:

        【解决方案3】:

        好像有人已经solved this on the linuxquestions forum了。

        来自论坛:

                    # This is part of Mail::POP3Client to get the headers and body of the POP3 mail in question
                    $body = $connection->HeadAndBody($i);
                    # Parse the message with MIME::Parser, declare the body as an entitty
                    $msg = $parser->parse_data($body);
                    # Find out if this is a multipart MIME message or just a plaintext
                    $num_parts=$msg->parts;
                    # So its its got 0 parts i.e. is a plaintext
                    if ($num_parts eq 0) {
                    # Get the message by POP3Client
                    $message = $connection->Body($i);
                    # Use this series of regular expressions to verify that its ok for MySQL
                    $message =~ s/</&lt;/g;
                    $message =~ s/>/&gt;/g;
                    $message =~ s/'//g;
                                          }
                    else {
                          # If it is MIME the parse the first part (the plaintext) into a string
                         $message = $msg->parts(0)->bodyhandle->as_string;
                          }
        

        【讨论】:

        • 你能修复指向 linuxquestions.org 的链接吗?
        • 嗯。如果我能打字我会很危险。固定。
        【解决方案4】:

        你在 perl Mail-Box-2.117 中有一个完整的例子:

        http://cpansearch.perl.org/src/MARKOV/Mail-Box-2.117/examples/strip-attachments.pl

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-11-28
          • 1970-01-01
          • 2014-08-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多