views:

620

answers:

5

Hey, I was wondering if there is a way to attach files (specifically .csv files) to a mail message in Perl without using MIME::Lite or any other libraries.

Right now, I have a 'mailer function' that works fine, but I'm not sure how to adapt it into attaching files. Here is what I have:

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: cheese\@yahoo.com\n";
print MAIL "From: queso\@what.com\n";
print MAIL "Subject: Attached is $filename\n\n";
print MAIL "$message";
close(MAIL);

I think this is specific for UNIX.

+2  A: 

General style tips to make your life easier:

Ie:

open my $mail, '|-', '/usr/sbin/sendmail', '-t'  or Carp::croak("Cant start sendmail, $! $@");

print $mail  "foo";

close $mail or Carp::croak("SendMail might have died! :( , $! $@");

perldoc -f open

Kent Fredric
+2  A: 
Silfverstrom
+11  A: 

Why do you want to write code that already exists? There's probably a much better way to solve your task than recreating bugs and maintaining more code yourself. Are you having a problem installing modules? There are ways that you can distribute third-party modules with your code, too.

If you want to do it yourself, you just have to do the same things the module does for you. You can just look at the code to see what they did. You just do that. It is open source after all. :)

brian d foy
I'm hesitant to install modules unless that can be done on a 'local' level somewhere inside the folder where the pl exists... i read a little within the link dave smith gave me, but im not sure if id rather install the module or just find the section of code in the source to handle attachments
CheeseConQueso
Look up local::lib, which makes this easy.
ijw
+7  A: 

If part of your problem is that you're on shared hosting and cannot install extra libraries, they can usually be installed in (and used from) a local a library (e.g., ~/lib). There are instructions for that over here (under "I don't have permission to install a module on the system!").

Dave W. Smith
A: 
print "To: ";       my $to=<>;      chomp $to;
print "From: ";     my $from=<>;    chomp $from;
print "Attach: ";   my $attach=<>;  chomp $attach;
print "Subject: ";  my $subject=<>; chomp $subject;
print "Message: ";  my $message=<>; chomp $message;

my $mail_fh = \*MAIL;
open $mail_fh, "|uuencode $attach $attach |mailx -m -s \"$subject\" -r $from $to";
print $mail_fh $message;
close($mail_fh);
CheeseConQueso
the message body doesn't print... not sure why yet, but the attachment works for (seemingly) any file type
CheeseConQueso