gcoates has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the MIME::Lite module to attach a CSV file to emails that are being sent out via sendmail. I'm having a problem though; the attachment is being truncating around 90 characters or so. Does anyone have any idea what's going on? Here's my code:
sub email_results { my %vars = $cgi->Vars(); my $msg = MIME::Lite->new ( From => $email, To => $email, Subject => 'Credit Card Survey Results', Type => 'multipart/mixed' ); my $body = (); my $csv = '"'; foreach my $key (sort keys %vars) { $body .= "$key: $vars{$key}\n"; $csv .= "$vars{$key}" . '","'; } chop($csv); chop($csv); $msg->attach ( Type => 'TEXT', Data => $body ); my $attachment = MIME::Lite->new ( Type => 'text/csv', Data => $csv, Disposition => 'attachment' ); $attachment->attr('content-type.name' => 'survey.csv'); $msg->attach($attachment) or die "Error attaching CSV file: $!"; $msg->send(); }

Replies are listed 'Best First'.
Re: MIME::Lite truncating attachment
by jethro (Monsignor) on Jul 01, 2008 at 02:00 UTC
    Did you try to change $msg->send() to $msg->print() ? This might give you a hint whether the error occurs while constructing or sending the message.

    I tested your sub (on linux, without sending the email, just with $msg->print(), some sample data and on the command line instead of on a web server) and it worked fine, nothing got truncated.

    I had Mime::Lite version 3.021 and got the following attachement header:

    MIME-Version: 1.0 Content-Disposition: attachment Content-Length: 1459 Content-Transfer-Encoding: binary Content-Type: text/csv; name="survey.csv" X-Mailer: MIME::Lite 3.021 (F2.76; T1.24; A2.03; B3.07_01; Q3.07) Date: Tue, 1 Jul 2008 03:53:31 +0200
Re: MIME::Lite truncating attachment
by pc88mxer (Vicar) on Jul 01, 2008 at 01:48 UTC
    Can you show us the raw mail message together with the GET or POST data that generated it?

    I notice that you are not properly quoting your CSV values. use the combine() method of Text::CSV to ensure that it is formatted correctly.

Re: MIME::Lite truncating attachment
by bradcathey (Prior) on Jul 01, 2008 at 12:27 UTC

    This is not an answer, but a question for anyone. What is happening here:

    my $body = (); my $csv = '"';

    in the above code? Wouldn't:

    my ( $body, $csv );

    suffice? Or is there some reason to declare the array ref $body if that is what's happening here? Thanks!

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      I first thought $body would be initialized to '0' as the size of the list similar to @b=(); $body=@b

      But $body gets initialized to the empty string. Seems (by running perl -D1 -e '$b=();') that () is interpreted as an empty term folding to a (wrong!?) boolean expression like $body=(1==0)

      my ( $body,$csv)=('','"'); would suffice, your code misses the initialization. If you run that without 'use strict' you won't get errors, but $csv is still missing the init to '"', so the first value in the $csv string would have no opening quotation mark

      Thanks for the tips, folks. I figured out what happened. The CGI module uses a null string (\0) to separate values in multi-valued parameters. Those nulls where choking the email program. Replacing the null strings with another separator fixed the problem.