我可以解析保存为文本文件的电子邮件并使用 Perl 检索/保存其附件吗?

发布于 2024-10-04 14:37:42 字数 908 浏览 0 评论 0原文

我正在使用 Perl & MAIL::IMAPClient 保存来自的电子邮件使用 IMAPClient 方法将 Gmail 作为 .txt 文件:

message_to_file

这些文件似乎包含编码为文本的电子邮件附件。

这是一些文本文件:

--0015174c1274ee7ca60495ca69d5
Content-Type: video/3gpp; name="20101112233055.3gp"
Content-Disposition: attachment; filename="20101112233055.3gp"
Content-Transfer-Encoding: base64
X-Attachment-Id: 1353288501407252480-1

AAAAHGZ0eXAzZ3A0AAADADNncDRtcDQxM2dwNgAFHyltZGF0AAABthAwrMK9/Mue7fM+95wsf9P8
WI7mPzzp/ikijbucv72j7OywVGuh5kBzo89Zra6PihxZg0zadDqihZFpsPJeG36Ihk9qZW+LLQ2u
NEd96vsqgpnLFnhhwGBWgL2Xpt0cXkW....[A LOT MORE CHARS]....AAAQAAAALAAAAAQAAABRzdHN6
AAAAAAAAACAAAAChAAAAIHN0Y28AAAAAAAAABAABHNoAASMaAALYFwAFHeU=
--0015174c1274ee7ca60495ca69d5--

我找不到任何单独保存附件的方法。有没有办法通过解析来做到这一点?

I'm using Perl & MAIL::IMAPClient to save emails from Gmail as .txt files using the IMAPClient method:

message_to_file

These files seem to contain the attachments of emails encoded as text.

Here's some of the text file:

--0015174c1274ee7ca60495ca69d5
Content-Type: video/3gpp; name="20101112233055.3gp"
Content-Disposition: attachment; filename="20101112233055.3gp"
Content-Transfer-Encoding: base64
X-Attachment-Id: 1353288501407252480-1

AAAAHGZ0eXAzZ3A0AAADADNncDRtcDQxM2dwNgAFHyltZGF0AAABthAwrMK9/Mue7fM+95wsf9P8
WI7mPzzp/ikijbucv72j7OywVGuh5kBzo89Zra6PihxZg0zadDqihZFpsPJeG36Ihk9qZW+LLQ2u
NEd96vsqgpnLFnhhwGBWgL2Xpt0cXkW....[A LOT MORE CHARS]....AAAQAAAALAAAAAQAAABRzdHN6
AAAAAAAAACAAAAChAAAAIHN0Y28AAAAAAAAABAABHNoAASMaAALYFwAFHeU=
--0015174c1274ee7ca60495ca69d5--

I can't find any method that will save the attachment separately. Is there any way to do this via parsing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2024-10-11 14:37:42

您得到的是 MIME 编码的电子邮件的原始文本。大多数语言都有一个通用 MIME 库来解析这些内容。在 CPAN 上快速搜索发现
MIME::Parser 可能可以解决问题:

use MIME::Parser;

open(FH, '/var/tmp/test.txt');
my $parser = new MIME::Parser;
my $entity = $parser->parse(\*FH) or die;

$entity->dump_skeleton;

这会将纯文本文件 test.txt 中存储的电子邮件的各个部分转储到 /var/tmp。查看 MIME::Parser 文档以获取更多选项。

What you've got there is the raw text of a MIME-encoded email message. Most languages have a general purpose MIME library for parsing these. A quick search on CPAN reveals that
MIME::Parser might do the trick:

use MIME::Parser;

open(FH, '/var/tmp/test.txt');
my $parser = new MIME::Parser;
my $entity = $parser->parse(\*FH) or die;

$entity->dump_skeleton;

This will dump the various parts of the email stored in the plain text file test.txt to /var/tmp. Check out the MIME::Parser docs for further options.

临风闻羌笛 2024-10-11 14:37:42

这是一个解决方法的建议。在经过标题之后,您将使用此逻辑。

use MIME::Base64;

my $attachlines = '';
while ( <$input> ) { 
    last if index( $_, '--0015174c1274ee7ca60495ca69d5--' ) == 0;
    $attachlines .= $_;
}

my $attach = MIME::Base64::decode( $attachlines );

注意:我想当然地认为您知道 MIME 多部分表单的工作原理,因此我不讨论如何以编程方式获取分隔符'--0015174c1274ee7ca60495ca69d5--'

This is a suggestion for a workaround. You would engage this logic after you were past the headers.

use MIME::Base64;

my $attachlines = '';
while ( <$input> ) { 
    last if index( $_, '--0015174c1274ee7ca60495ca69d5--' ) == 0;
    $attachlines .= $_;
}

my $attach = MIME::Base64::decode( $attachlines );

Note: I take for granted that you know how MIME multi-part forms work, so I don't discuss how to programmatically get the divider '--0015174c1274ee7ca60495ca69d5--'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文