Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Email Text File

by PerlSufi (Pilgrim)
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\@awesome.com", To=> "me\@awesome.com", 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 :)
PerlSufi

Comment on Email Text File
Download Code
Re: Email Text File
by marto (Chancellor) 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'.
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/vnd.ms-excel', 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...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-08-20 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (110 votes), past polls