Advertisement
CoolRaoul

my_sendmail v3

Mar 2nd, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.34 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. #
  4. #   my_sendmail [-m <mail server>]
  5. #           [-s <sujet>]
  6. #           [-f <from>]
  7. #           [-r <reply-to>]
  8. #           [-h <Hello String>]
  9. #           [-c <cc-dests>]
  10. #           [-C <charset>]
  11. #           [-D]
  12. #           [-t <type mime du contenu>]
  13. #           [-M <type mime des documents joints>]
  14. #           [-b <bcc-dests>] <dests> [<attached files>...]
  15. #
  16. #-
  17.  
  18. use Net::SMTP;
  19. use MIME::Base64;
  20. use Getopt::Std;
  21. use File::Basename;
  22. use Sys::Hostname;
  23.  
  24. my $progname;
  25. ( $progname = $0 ) =~ s/^.*\///;
  26.  
  27. %ctypes = (
  28.  
  29.        'tar' => 'application/x-tar',
  30.        'man' => 'application/zip',
  31.        'au|snd' => 'audio/basic',
  32.        'mpg|mp3' => 'audio/mpeg',
  33.        'mp4' => 'audio/mp4',
  34.        'aif|aiff|aifc' => 'audio/x-aiff',
  35.        'wav' => 'audio/x-wav',
  36.        'gif' => 'image/gif',
  37.        'jpg|jpeg|jpe' => 'image/jpeg',
  38.        'png' => 'image/png',
  39.        'tiff|tif' => 'image/tiff',
  40.        'pbm' => 'image/x-portable-bitmap',
  41.        'pgm' => 'image/x-portable-graymap',
  42.        'ppm' => 'image/x-portable-pixmap',
  43.        'zip' => 'application/x-zip',
  44.        'gz|gzip' => 'application/x-gzip',
  45.        'css' => 'text/css',
  46.        'csv' => 'text/csv',
  47.        'htm|html' => 'text/html',
  48.        'txt|g|h|c|cc|hh|m|f90' => 'text/plain',
  49.        'rtx' => 'text/richtext',
  50.        'rtf' => 'text/rtf',
  51.        'tsv' => 'text/tab-separated-value',
  52.        'xml' => 'text/xml',
  53.        'h264' => 'video/h264',
  54.        'dv' => 'video/dv',
  55.        'mpeg|mpg|mpe' => 'video/mpeg',
  56.        'qt|mov' => 'video/quicktime',
  57.        'avi' => 'video/msvideo',
  58.        'pdf' => 'application/pdf',
  59. );
  60.  
  61. #+
  62. #   parsing des options
  63. #-
  64. getopts('s:f:m:c:b:vM:r:t:C:h:D', \%opts) or &usage;
  65. my $to = shift or &usage;
  66. my $verbose = $opts{v};
  67. my $subject = $opts{s};
  68. my $charset = $opts{C} || "ISO-8859-1"; # could be "UTF-8"
  69. my @files = ();
  70. while ($_= shift) {
  71.   push @files,$_;
  72. }
  73.  
  74. my $from;
  75.  
  76. $from = $opts{f} || (($ENV{USER} || $ENV{LOGNAME} || "unknown") . "\@" . hostname);
  77.  
  78.  
  79. my $smtphost = $opts{m} || "localhost"; # SMTP mail host
  80.  
  81. #+
  82. #   séparateur multipart quasi aléatoire
  83. #-
  84. my $boundary = '<------------ ';
  85. my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
  86. foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; }
  87. $boundary .= ' ------------>';
  88.  
  89. #+
  90. #   initialisation dialogue smtp
  91. #-
  92. my %options = (
  93.     Timeout => 30,
  94. );
  95. $options{Hello} = $opts{h} if $opts{h} ;
  96. $options{Debug} = 1 if $opts{D} ;
  97.  
  98. my $smtp = Net::SMTP->new($smtphost, %options ) or die "Net::SMTP::new: $!\n";
  99. $smtp->mail($from);
  100. foreach (split /,/, $to) {
  101.   $smtp->to($_);
  102. }
  103. if (my $cclist = $opts{c}) {
  104.   foreach (split /,/, $cclist) {
  105.     $smtp->cc($_);
  106.   }
  107. }
  108. if (my $cclist = $opts{b}) {
  109.   foreach (split /,/, $cclist) {
  110.     $smtp->bcc($_);
  111.   }
  112. }
  113.  
  114. #+
  115. #   en-tetes message
  116. #-
  117. $smtp->data();
  118.  
  119. $smtp->datasend("From: $from\n");
  120. $smtp->datasend("To: $to\n");
  121. if ($opts{c}) {
  122.   $smtp->datasend("Cc: $opts{c}\n");
  123. }
  124.  
  125. if (exists($opts{r})) {
  126.   $smtp->datasend("Reply-To: $opts{r}\n");
  127. }
  128.  
  129. $smtp->datasend("Subject: $subject\n") if $subject;
  130. $smtp->datasend("MIME-Version: 1.0\n");
  131. $smtp->datasend("X-Mailer: $progname (MY)\n");
  132. $smtp->datasend("Content-Type: multipart/mixed; boundary=\"$boundary\"\n");
  133. $smtp->datasend("\n");
  134. $smtp->datasend("This is a multipart MIME-coded message\n");
  135. $smtp->datasend("\n");
  136. $smtp->datasend("\n");
  137.  
  138. #+
  139. #   partie textuelle du message (a partir de stdin)
  140. #-
  141. my $ctype = $opts{t} ? $opts{t} : "text/plain";
  142. $smtp->datasend("--$boundary\n");
  143.  
  144. $smtp->datasend("Content-Type: $ctype; charset=$charset\n");
  145.  
  146. $smtp->datasend("Content-Transfer-Encoding: base64\n");
  147. #$smtp->datasend("Content-Transfer-Encoding: 8bit\n");
  148.  
  149. $smtp->datasend("\n");
  150. while (<>) {
  151. #  $smtp->datasend($_);
  152.   $smtp->datasend(encode_base64($_));
  153. }
  154.  
  155. #+
  156. #   quelques lignes blanches pour aérer
  157. #-
  158. $smtp->datasend("\n\n\n");
  159.  
  160. #+
  161. #   boucle sur les fichiers à attacher
  162. #-
  163. foreach $file (@files) {
  164.   my $name = basename($file);
  165.   my $extension = (fileparse($file,qr/\.[^.]*?/))[2];
  166.   my $ct;
  167.  
  168.   # on tente de déterminer le content type
  169.   if ($opts{M}) {
  170.     $ct=$opts{M};
  171.   } else {
  172.     $ct = "Application/octet-stream";
  173.     foreach $type_extensions (keys %ctypes) {
  174.       if ($extension =~ /^\.($type_extensions)$/i) {
  175.     $ct = $ctypes{$type_extensions};
  176.     last;
  177.       }
  178.     }
  179.   }
  180.   $smtp->datasend("--$boundary\n");
  181.   $smtp->datasend("Content-Type: $ct; name=\"$name\"\n");
  182.   $smtp->datasend("Content-Disposition: attachment; filename=\"$name\"\n");
  183.   $smtp->datasend("Content-Transfer-Encoding: base64\n");
  184.   $smtp->datasend("\n");
  185.   open FILE,$file or die "fichier $file introuvable\n";
  186.   while (read(FILE, $buf, 60*57)) {
  187.     $smtp->datasend( encode_base64($buf));
  188.   }
  189.   print STDERR "$progname: attachement fichier $name type $ct\n" if $verbose;
  190. }
  191.  
  192. $smtp->datasend("--$boundary--\n");
  193. $smtp->dataend();
  194.  
  195. sub usage {
  196.   print STDERR <<'EOD';
  197. my_sendmail: envoi de mails avec documents attachés.
  198.  
  199. Usage:
  200.         my_sendmail     [-m <mail server>]  
  201.                         [-s <sujet>]
  202.                         [-f <from>]
  203.             [-r <reply-to>]
  204.             [-h <Hello String>]
  205.                         [-c <cc-dests>]
  206.                         [-C <charset>]
  207.                         [-M <mime type des fichiers joints>]
  208.                         [-t <mime type du corps du messages>]
  209.                         [-D] (debug)
  210.                         [-v]
  211.                         [-b <bcc-dests>] <dests> [<attached files>...] < <fichier contenant le texte du message>
  212. EOD
  213.   exit 1;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement