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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 10:16:00

问题


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.


回答1:


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. :)




回答2:


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!").




回答3:


General style tips to make your life easier:

  • use lexical file handles
  • use 3-arg-open
  • check return values

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




回答4:


you can specify the mail-headers as :

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

Look at an email sent with an attachment, that should help you out.

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




回答5:


Example - Email a zipped 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'



回答6:


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


来源:https://stackoverflow.com/questions/911231/how-can-i-send-email-attachment-without-using-an-additional-library-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!