Perl:发送zip作为base64编码的附件会破坏存档(Perl: sending zip as base64

编程入门 行业动态 更新时间:2024-10-28 19:31:45
Perl:发送zip作为base64编码的附件会破坏存档(Perl: sending zip as base64-encoded attachment corrupts archive)

我正在使用perl来组装一个multipart-MIME电子邮件,然后我在Windows环境中使用sendmail发送它。 我知道,不理想。

部分原因是收集文件,压缩文件,然后将zip文件编码为Base64,并将其作为附件写入电子邮件。 我的问题是,每当我发送超过一定大小的文件(我不确切知道大小是多少;介于20 KB和2 MB之间)时,zip文件最终会在收到时损坏。 (当在WinRAR中打开时,它会抱怨“意外结束归档”,并且CRC值都是零值,如果这是任何线索)。

我怀疑我可能只是将它写入电子邮件中,这样我就让垃圾或复制进入,但我无法看到它发生在哪里。 这是我用来进行阅读/编码/写作的片段; 它使用MIME :: Base64,显然有一个打开的Sendmail句柄。

open(FILE, "c:\\temp\\$uid.zip") or die "$!"; while (read(FILE, $buffer, 60*57)) { printf SENDMAIL encode_base64($buffer); }

即使我在没有任何缓冲的情况下阅读它(我当然应该有足够的内存用于一个微不足道的2mb文件),我仍然收到一个损坏的zip文件。 大小肯定是混淆因素,但我正在努力弄清楚为什么或如何补救它。

I'm using perl to assemble a multipart-MIME email, which I'm then sending using sendmail, on a Windows environment. Not ideal, I know.

Part of this is collecting files, zipping them up, then encoding the zip file as Base64 and writing it into the email as an attachment. My problem is that whenever I send files over a certain size (I don't know exactly what size that is; somewhere between 20 KB and 2 MB) the zip file ends up corrupted on receipt. (When opened in WinRAR it complains "unexpected end of archive", and the CRC values are all zero-valued, if that's any clue).

I suspect I might just be writing it into the email in such a way that I'm letting garbage or duplication in, but I can't see where it's happening. Here's the snippet I'm using to do the reading/encoding/writing; it's using MIME::Base64, and obviously has an open Sendmail handle.

open(FILE, "c:\\temp\\$uid.zip") or die "$!"; while (read(FILE, $buffer, 60*57)) { printf SENDMAIL encode_base64($buffer); }

Even when I read it in without any kind of buffering (I should certainly have enough memory for a paltry 2mb file), I still end up receiving a corrupted zip file. Size is definitely the confounding factor, but I'm struggling to figure out why or how to remedy it.

最满意答案

我认为在Windows上你需要binmode你的文件

open(FILE, "c:\\temp\\$uid.zip") or die "$!"; binmode FILE; while (read(FILE, $buffer, 60*57)) { printf SENDMAIL encode_base64($buffer); }

另外,使用MIME :: Lite发送电子邮件。

I think on Windows you need to binmode your file

open(FILE, "c:\\temp\\$uid.zip") or die "$!"; binmode FILE; while (read(FILE, $buffer, 60*57)) { printf SENDMAIL encode_base64($buffer); }

Also, use MIME::Lite for sending emails.

更多推荐

本文发布于:2023-07-08 05:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:附件   zip   Perl   sending

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!