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

Email Text File

by PerlSufi (Friar)
on Jun 28, 2013 at 16:48 UTC ( #1041286=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I wanted to attached a text file to an email after some file handling stuff. The problem is, the file is not attaching. Here is my sample code:
use MIME::Entity; my $file = "$ARGV[0]"; open (my $fh, '<', $file) or die "Could not open file: $!"; my $out = "fixed_"."$ARGV[0]"; open (my $fhout, '>', $out) or die "Could not open file: $!"; while (<$fh>) { file handling stuff here... } close($fh); close($fhout); my $report = MIME::Entity->build(Type=>"multipart/mixed", From=> "me\", To=> "me\", Subject=> "Formatted Report- "."$ARGV[0]"); $report->attach( Type=>'text/plain', Encoding=> 'base64', Path=> "$ENV{HOME}", Filename=> "fixed_"."$ARGV[0]" ); open MAIL, "|/usr/sbin/sendmail -t"; $report->print(\*MAIL); close MAIL;

Any insight is greatly appreciated :)

Replies are listed 'Best First'.
Re: Email Text File
by runrig (Abbot) on Jun 28, 2013 at 17:46 UTC

    Path should be the full path to the file (not just the directory). Filename is the 'recommended filename' that it gets attached as, and you can omit that.

    Also, I'm pretty sure you can just do $report->send('sendmail');.
      Thanks, runrig. I'm still getting problems sending it but I'll keep trying and post my code when I can
      I ended up saving it as an .xls file instead (since I needed to open it with that anyways) and changed the code to be
      $report->attach( Type=>'application/', Encoding=> 'base64', Path=> "/my/home/"."$out", Filename=> "$file"."_fixed.xls" );
      I feel like it may have been something with the encoding? I'm not sure :\
        Please break the habit of the "Useless use of quotes", i.e. uslessly double quoting lone variables...
Re: Email Text File
by marto (Archbishop) on Jun 28, 2013 at 17:34 UTC

    Avoid making life difficult for yourself, use something like MIME::Lite, plenty of examples on how to send emails with attachments. The author states there are problems with the module, I've never experienced any in years of using it. YMMV

      MIME::Lite doesn't really make things any easier in this case. I keep meaning to use MIME::Entity, but sometimes I forget and use MIME::Lite for no good reason. Though the one 'advantage' of MIME::Lite is that it will try to guess at the type of attachment if that option is set to 'AUTO'.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041286]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-06-21 13:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.