Perl: sending zip as base64-encoded attachment corrupts archive
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.
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.
精彩评论