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

北城以北 提交于 2019-12-01 11:15:58

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

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

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

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

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