How can I send an email attachment without using an additional library in Perl?

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.

I currently have a "mailer" function that works great, but I'm not sure how to adapt it to attach files. Here's 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 UNIX specific.

+1


a source to share


6 answers


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);

      



-1


a source


Why do you want to write existing code? There is probably a much better way to solve your problem than to recreate the errors and maintain more code yourself. Have a problem installing modules? There are ways you can also distribute third party modules with your code.



If you want to do it yourself, you just need to do exactly what the module does for you. You can just look at the code to see what they did. You just do it. It's open source. :)

+12


a source


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

+7


a source


General tips for creating your life:

Those:

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

+2


a source


you can specify post headers like:

Content-Type ie: image / jpeg; name = "file.jpg"
Content-Disposition (ie) attachment; filename = "name.jpg"
Content-Transfer-Encoding (ie) base64

Take a look at the email sent with an attachment that should help you.

the trick is multiple boundaries.
http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

+1


a source


Example. Send the zip file as an attachment:

base64 /path/to/my/file.zip | mail -s "Subject" recipient@mydomain.com -a 'Content-Type: application/zip; name="myfile.zip"' -a 'Content-Disposition: attachment' -a 'Content-Transfer-Encoding: base64'

      

+1


a source







All Articles