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.
a source to share
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);
a source to share
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. :)
a source to share
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!").
a source to share
General tips for creating your life:
- use lexical files
- use 3-arg-open
- check return values
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! :( , $! $@");
a source to share
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
a source to share