Perl: sending zip as root archive of base64 encoded attachments

I use perl to build multipart-MIME email, which I send using sendmail in a Windows environment. I don’t know, I know.

Part of this process is collecting files, copying them, and then encoding the zip file as Base64 and writing it to an email as an attachment. My problem is that whenever I send files to a specific size (I don’t know exactly what size, somewhere between 20 KB and 2 MB), the zip file ends when it is received. (When it opens in WinRAR, it complains about the "unexpected end of the archive", and the CRC values ​​are zero if it's some kind of key).

I suspect that I can simply write it in an email in such a way that I allow garbage or duplication, but I do not see where this is happening. Here is a snippet that I use for reading / encoding / writing; it uses MIME :: Base64 and obviously has an open Sendmail descriptor.

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

Even when I read it without any buffering (I should have enough memory for a tiny 2mb file), I still get a corrupted zip file. Size is definitely a confounding factor, but I'm struggling to figure out why and how to fix it.

+3
source share
1 answer

I think on windows you need 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 to send emails.

+4
source

All Articles