home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Multipart.pm < prev    next >
Encoding:
Perl POD Document  |  2002-11-13  |  7.1 KB  |  291 lines

  1. # @(#) Multipart.pm - <DESCRIPTION>
  2. #
  3. # Author:
  4. #      Dave Roberts
  5. #
  6. # Synopsis:
  7. #      Multipart.pm
  8. #
  9. # Version
  10. #      $Source: D:/src/perl/Net/SMTP/RCS/Multipart.pm $
  11. #      $Revision: 1.5 $
  12. #      $State: Exp $
  13. #
  14. # Description:
  15. #      <FULL DESCRIPTION>
  16. #
  17. #******************************************************************************
  18. package Net::SMTP::Multipart;
  19.  
  20. use strict;
  21. use vars qw($VERSION @ISA);
  22. use Carp;
  23. use MIME::Base64;
  24. use Net::SMTP;
  25.  
  26. @ISA = qw(Net::SMTP);
  27.  
  28. our($b);
  29.  
  30. our $VERSION = sprintf("%d.%d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
  31.  
  32.  
  33. sub new {
  34.     my $c          = shift;         # What class are we constructing?
  35.     my $classname  = ref($c) || $c;
  36.     my $self       = $classname->SUPER::new(@_);
  37.     $self->_init(@_) if defined ($self);
  38.     return $self;                   # And give it back
  39. }
  40.  
  41. sub _init {
  42.     my $self = shift;
  43.     # Create arbitrary boundary text
  44.     my ($i,$n,@chrs);
  45.     $b = "";
  46.     foreach $n (48..57,65..90,97..122) { $chrs[$i++] = chr($n);}
  47.     foreach $n (0..20) {$b .= $chrs[rand($i)];}
  48. }
  49.  
  50. sub Header {
  51.     my $self = shift;
  52.     my %arg  = @_;
  53.       carp 'Net::SMTP::Multipart:Header: must be called with a To value' unless $arg{To};
  54.       carp 'Net::SMTP::Multipart:Header: must be called with a Subj value' unless $arg{Subj};
  55.       carp 'Net::SMTP::Multipart:Header: must be called with a From value' unless $arg{From};
  56.       $self->mail($arg{From});  # Sender Mail Address
  57.     $self->to($arg{To});    # Recpient Mail Address
  58.     $self->data();
  59.     $self->datasend("To: $arg{To}\n");
  60.     $self->datasend("Subject: $arg{Subj}\n");
  61.     $self->datasend("MIME-Version: 1.0\n");
  62.     $self->datasend(sprintf "Content-Type: multipart/mixed; BOUNDARY=\"%s\"\n",$b);
  63. }
  64.  
  65. sub Text {
  66.     my $self = shift;
  67.     $self->datasend(sprintf"\n--%s\n",$b);
  68.     $self->datasend("Content-Type: text/plain\n");
  69.     foreach my $text (@_) {
  70.       $self->datasend($text);
  71.     }
  72.     $self->datasend("\n\n");
  73. }
  74.  
  75. sub FileAttach {
  76.     my $self = shift;
  77.     foreach my $file (@_) {
  78.       unless (-f $file) {
  79.         carp 'Net::SMTP::Multipart:FileAttach: unable to find file $file';
  80.         next;
  81.       }
  82.       my($bytesread,$buffer,$data,$total);
  83.       open(FH,"$file") || carp "Net::SMTP::Multipart:FileAttach: failed to open $file\n";
  84.       binmode(FH);
  85.       while ( ($bytesread=sysread(FH,$buffer, 1024))==1024 ){
  86.         $total += $bytesread;
  87.         # 500K Limit on Upload Images to prevent buffer overflow
  88.         #if (($total/1024) > 500){
  89.         #  printf "TooBig %s\n",$total/1024;
  90.         #  $toobig = 1;
  91.         #  last;
  92.         #}
  93.         $data .= $buffer;
  94.       }
  95.       if ($bytesread) {
  96.         $data .= $buffer;
  97.         $total += $bytesread ;
  98.       }
  99.       #print "File Size: $total bytes\n";
  100.       close FH;
  101.  
  102.       if ($data){
  103.         $self->datasend("--$b\n");
  104.         $self->datasend("Content-Type: ; name=\"$file\"\n");
  105.         $self->datasend("Content-Transfer-Encoding: base64\n");
  106.         $self->datasend("Content-Disposition: attachment; =filename=\"$file\"\n\n");
  107.         $self->datasend(encode_base64($data));
  108.         $self->datasend("--$b\n");
  109.       }
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. sub End {
  116.     my $self = shift;
  117.     $self->datasend(sprintf"\n--%s--\n",$b);                 # send boundary end message
  118.     foreach my $epl (@_) {
  119.       $self->datasend("$epl");                               # send epilogue text
  120.     }
  121.     $self->datasend("\n");                                   # send final carriage return
  122.     $self->dataend();                                        # close the message
  123.     return $self->quit();                                    # quit and return the status
  124. }
  125.  
  126.  
  127. sub mail {
  128.     my $self = shift;
  129.     $self->SUPER::mail(@_);
  130. }
  131.  
  132. sub to {
  133.     my $self = shift;
  134.     $self->SUPER::to(@_);
  135. }
  136.  
  137. sub data {
  138.     my $self = shift;
  139.     $self->SUPER::data(@_);
  140. }
  141.  
  142. sub datasend {
  143.     my $self = shift;
  144.     #printf "datasend: %s\n",@_;
  145.     $self->SUPER::datasend(@_);
  146. }
  147. sub dataend {
  148.     my $self = shift;
  149.     $self->SUPER::dataend();
  150. }
  151.  
  152. sub quit {
  153.     my $self = shift;
  154.     $self->SUPER::quit(@_);
  155. }
  156.  
  157.  
  158.  
  159.  
  160. 1;
  161.  
  162. __END__
  163.  
  164. =head1 NAME
  165.  
  166.     Multipart.pm
  167.  
  168. =head1 SYNOPSIS
  169.  
  170.   $smtp = Net::SMTP::Multipart->new("mailrelay.someco.com");
  171.   $smtp->Header(To   => "someone\@someco.com",
  172.                 Subj => "Multipart Mail Demo",
  173.                 From => "me\@someco.com");
  174.   $smtp->Text("This is the first text part of the message");
  175.   $smtp->FileAttach("c:/tmp/myfile.xls");
  176.   $smtp->End();
  177.  
  178. =head1 DESCRIPTION
  179.  
  180. This module uses the Net::SMTP and Mime::Base64 modules to compose and send
  181. multipart mail messages.  It uses the Net::SMTP methods, but simplifies formatting
  182. of multipart messages using its internal methods Header, Text, FileAttach and End.
  183.  
  184. =head1 METHODS
  185.  
  186. =over 2
  187.  
  188. =item B<new>
  189.  
  190. The B<new> method invokes a new instance of the Net::SMTP::Multipart class, using the same
  191. arguments as the parent method.
  192.  
  193.  
  194. =item B<Header>
  195.  
  196. The B<Header> method creates the header of the multipart message.  It should be called with
  197. the following arguments
  198.  
  199. =over 4
  200.  
  201. =item B<To>
  202.  
  203. an array of mail addresses to which the mail is to be sent
  204.  
  205. =item B<From>
  206.  
  207. the mail address from which the mail is sent
  208.  
  209. =item B<Subj>
  210.  
  211. the subject title of the mail
  212.  
  213. =back
  214.  
  215. =item B<Text>
  216.  
  217. This method generates a text part to the message.  The argument provided is treated as text and
  218. populates the text part of the message.
  219.  
  220. =item B<FileAttach>
  221.  
  222. This method includes a file (identified in the argument when this is called) within an encoded
  223. part of the message.
  224.  
  225. =item B<End>
  226.  
  227. This method generates an epilogue part to the message.  The argument provided is treated as text and
  228. populates the epilogue (which most mail agents do not display).  The mail message is then sent and
  229. the class instance destroyed.
  230.  
  231. =back
  232.  
  233. =head1 REQUIRED MODULES
  234.  
  235. C<Carp>
  236.  
  237. C<MIME::Base64>
  238.  
  239. C<Net::SMTP>
  240.  
  241. C<strict>
  242.  
  243. C<vars>
  244.  
  245. =head1 SEE ALSO
  246.  
  247. =head1 EXAMPLES
  248.  
  249. =head1 TO DO
  250.  
  251. =head1 AUTHOR
  252.  
  253. Dave Roberts
  254.  
  255. =head1 SUPPORT
  256.  
  257. You can send bug reports and suggestions for improvements on this module
  258. to me at DaveRoberts@iname.com. However, I can't promise to offer
  259. any other support for this script.
  260.  
  261. =head1 COPYRIGHT
  262.  
  263. This script is Copyright ⌐ 2002 Dave Roberts. All rights reserved.
  264.  
  265. This script is free software; you can redistribute it and/or modify it
  266. under the same terms as Perl itself. This script is distributed in the
  267. hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  268. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  269. PURPOSE. The copyright holder of this script can not be held liable
  270. for any general, special, incidental or consequential damages arising
  271. out of the use of the script.
  272.  
  273. =head1 CHANGE HISTORY
  274.  
  275. $Log: Multipart.pm $
  276. Revision 1.5  2002/11/11 12:12:18  Dave.Roberts
  277. corrected bug in documentation synopsis - changed
  278. Net::SMTP::MultiPart to Net::SMTP::Multipart
  279.  
  280. Revision 1.4  2002/04/05 11:36:33  Dave.Roberts
  281. change to version number generation code
  282.  
  283. Revision 1.3  2002/03/27 09:16:29  Dave.Roberts
  284. initial pod added
  285.  
  286. Revision 1.2  2002/03/26 12:03:23  Dave.Roberts
  287. added basic pod structure
  288.  
  289.  
  290. =cut
  291.