我可以解析保存为文本文件的电子邮件并使用 Perl 检索/保存其附件吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您得到的是 MIME 编码的电子邮件的原始文本。大多数语言都有一个通用 MIME 库来解析这些内容。在 CPAN 上快速搜索发现
MIME::Parser 可能可以解决问题:
这会将纯文本文件 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:
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.
这是一个解决方法的建议。在经过标题之后,您将使用此逻辑。
注意:我想当然地认为您知道 MIME 多部分表单的工作原理,因此我不讨论如何以编程方式获取分隔符
'--0015174c1274ee7ca60495ca69d5--'
。This is a suggestion for a workaround. You would engage this logic after you were past the headers.
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--'
.