Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

MIME::Lite behaving strange

by bar10der (Beadle)
on Apr 01, 2009 at 13:32 UTC ( #754699=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I want my script to email report as an attachment. No error is reported and email is sent with attachment but when attached file is opened, it is all garbage!! Here is my code-
#!/usr/bin/perl -w use MIME::Lite; #use strict; use warnings; my $mail_host = 'server name'; my $to="email"; my $from="fromServer"; my $subject="Reports"; my $attach = "daily_summary.csv"; my $body = "See attached reports.\n"; ### Create the multipart container my $msg = MIME::Lite->new ( From => $from, To => $to, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; ### Add the text message part $msg->attach ( Type => 'TEXT', Data => $body ) or die "Error adding the text message part: $!\n"; ### Add the file $msg->attach ( Type => 'text/csv', Path => 'csv file path', Filename => $attach, Disposition => 'attachment' ) or die "Error adding $attach : $!\n"; ### Send the Message MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send;
Any help will be greatly appreciated. Thanks

Replies are listed 'Best First'.
Re: MIME::Lite behaving strange
by jethro (Monsignor) on Apr 01, 2009 at 14:34 UTC

    First of all, 'use strict' does not work if you put a '#' before it

    I tried your code with my values for mailserver, recipient etc., but had to change my $from="bla@xyz"; to my $from='bla@xyz'; because if you use an address with '@' in it, you better not interpolate the string.

    Sent it and it worked. Attachement came in flawlessly (if I didn't use any special characters).

    Could it be that in your csv file there are 8-bit characters, for example umlauts? Usually you need something like $part->attr('content-type.charset' => 'UTF-8'); to identify the right character encoding

    You could look a bit closer at the garbage you are receiving (compare it to the source file) or post it here. In most cases it is not random garbage but gives broad hints about what happened to the source file. Was it base64 encoded or maybe packed?

    UPDATE: added info about character encoding

      I have changed the code and this is how it looks now-
      #!/usr/bin/perl -w use MIME::Lite; use strict; use warnings; my $mail_host = 'servername.com'; my $to='fname.lname@sun.com'; my $from="west"; my $subject="Reports"; my $body = "See attached reports.\n"; ### Create the multipart container my $msg = MIME::Lite->new ( From => $from, To => $to, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; ### Add the text message part $msg->attach ( Type => 'TEXT', Data => $body ) or die "Error adding the text message part: $!\n"; ### Add the file $msg->attach ( Type => 'text/plain; charset=UTF-8', Path => '/dir/dir/dir/report', Filename => 'test.txt', Disposition => 'attachment' ) or die "Error adding untouched_tickets.csv: $!\n"; ### Send the Message MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send;

      Content of the test.txt on server is -

      this is a test message

      However content of the test.txt which came through email is -

      mkStats.pl CheckDB.pl CleanUp.pl reports.pl
      So the attachment is containing these 4 file names everytime I send email, no matter which file I am attaching.

        You set 'Filename' to test.txt, but Filename is only how the file is called in the attachment. The file itself should be found in 'Path'. So Path     => '/path/to/the/file/test.txt' (or Path => 'test.txt' if the file is in the current path) would be more appropriate to get anything useful (with appropriate values for /path/to/the/file)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://754699]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2021-09-26 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?