Search code examples
perlmod-perl2

attached file is blank on the sent email


I have used this script to send the text file, the email go out with attachment but when I open the attached file it's blank. any idea why? did I miss anything. thx

#!/usr/bin/perl -wl
my $msg = MIME::Lite->new(
    From    => 'xxx.net',
    To      => 'xxx.com',
    Subject => 'Report',
    Type    => 'multipart/mixed',
)or die "Error creating multipart container: $!\n";
$msg->attach(
    Type     => 'TEXT',
    Data     => " Please check the attached file.",
)or die "Error adding the text message part: $!\n";
$msg->attach (
   Type => 'text/plain',
   Path => '/myfile/file1',
   Filename => 'result.txt',
   Disposition => 'attachment'
)or die "Error adding the attached file part: $!\n" ;
$msg->send;

Solution

  • You're a little confused about the arguments to attach. From the fine manual:

    Filename
    Optional. The name of the attachment. You can use this to supply a recommended filename for the end-user who is saving the attachment to disk. You only need this if the filename at the end of the "Path" is inadequate, or if you're using "Data" instead of "Path". You should not put path information in here (e.g., no "/" or "\" or ":" characters should be used).

    [...]

    Path
    Alternative to "Data" or "FH". Path to a file containing the data... actually, it can be any open()able expression. If it looks like a path, the last element will automatically be treated as the filename. See "ReadNow" also.

    The Path is the full path to the file that you want to attach, the Filename is the name that you want to receiver to see for that file.

    I think you want this:

    $msg->attach (
       Type => 'text/plain',
       Path => '/myfile/file1/result.txt',
       Filename => 'result.txt',
       Disposition => 'attachment'
    ) or die "Error adding the attached file part: $!\n" ;