PERL   19

SmartHost

Guest on 18th August 2022 01:16:20 AM

  1. package Mail::SpamAssassin::SMTP::SmartHost;
  2.  
  3. require 5.001;
  4.  
  5. use strict;
  6. use vars qw($VERSION @ISA @EXPORT);
  7.  
  8. require Exporter;
  9. require AutoLoader;
  10. use Carp;
  11. use Net::DNS;
  12. use Net::SMTP;
  13.  
  14. @ISA = qw(Exporter AutoLoader);
  15. @EXPORT = qw();
  16.  
  17. $VERSION = '1.1';
  18.  
  19. sub _smarthost {
  20.     my $self = shift;
  21.     my $target;
  22.  
  23.     my $client = new Net::SMTP($self->{SMARTHOST});
  24.     $client->mail($self->{FROM});
  25.     foreach $target (@{$self->{TO}}) {
  26.         $client->to($target)
  27.     }
  28.     $client->data($self->{MSG});
  29.     $client->quit;
  30. }
  31.  
  32. # New instance.
  33. sub new {
  34.     my($this, $tmpto) = undef;
  35.  
  36.     $this = $_[0];
  37.    
  38.     my $class = ref($this) || $this;
  39.     my $self = {};
  40.  
  41.     $self->{FROM} = $_[1];
  42.     $self->{TO} = $_[2];
  43.     $self->{MSG} = $_[3];
  44.     $self->{SMARTHOST} = $_[4];
  45.  
  46.     bless($self, $class);    
  47.     croak("Bad format.") unless defined($self->{SMARTHOST});
  48.    
  49.     $self->_smarthost;
  50.  
  51.     return $self;
  52. }
  53.  
  54. 1;
  55. __END__
  56. # POD begins here.
  57.  
  58. =head1 NAME
  59.  
  60. Mail::SpamAssassin::SMTP::SmartHost - A simple smarthost module for Net::SMTP::Server.
  61.  
  62. =head1 SYNOPSIS
  63.  
  64.   use Carp;
  65.   use Net::SMTP::Server;
  66.   use Net::SMTP::Server::Client;
  67.   use Mail::SpamAssassin::SMTP::SmartHost;
  68.  
  69.   $smarthost="localhost:10026";
  70.  
  71.   $server = new Net::SMTP::Server('localhost', 25) ||
  72.     croak("Unable to handle client connection: $!\n");
  73.  
  74.   while($conn = $server->accept()) {
  75.     # We can perform all sorts of checks here for spammers, ACLs,
  76.     # and other useful stuff to check on a connection.
  77.  
  78.     # Handle the client's connection and spawn off a new parser.
  79.     # This can/should be a fork() or a new thread,
  80.     # but for simplicity...
  81.     my $client = new Net::SMTP::Server::Client($conn) ||
  82.         croak("Unable to handle client connection: $!\n");
  83.  
  84.     # Process the client.  This command will block until
  85.     # the connecting client completes the SMTP transaction.
  86.     $client->process || next;
  87.    
  88.     # In this simple server, we're just relaying everything
  89.     # to a server.  If a real server were implemented, you
  90.     # could save email to a file, or perform various other
  91.     # actions on it here.
  92.     my $relay = new Mail::SpamAssassin::SMTP::SmartHost($client->{FROM},
  93.                                                  $client->{TO},
  94.                                                  $client->{MSG},
  95.                                                  $smarthost);
  96.   }
  97.  
  98. =head1 DESCRIPTION
  99.  
  100. The Mail::SpamAssassin::SMTP::SmartHost module implements simple SMTP client
  101. connection for use with the Net::SMTP::Server module.  All this module does
  102. is to take a given message and deliver it into another SMTP server, using it
  103. as a "smarthost", making it useful for reinjecting filtered content back
  104. into an SMTP server via an unfiltered port.
  105.  
  106. This code started life as the Net::SMTP::Server::Relay module which comes
  107. standard with the Net::SMTP::Server package.  After some appropriate
  108. modifications, it is now useful to connect to an arbitrary SMTP server.
  109.  
  110. The above example illustrates the use of the Mail::SpamAssassin::SMTP::SmartHost
  111. module -- you simply have to instantiate the module, passing along
  112. the sender, recipients, message, and next-hop mailserver.  More formally:
  113.  
  114.   $relay = new Mail::SpamAssassin::SMTP::SmartHost($from, @to, $msg, $smarthost);
  115.  
  116. Where $from is the sender, @to is an array containing the list of
  117. recipients, $msg is the message to relay, and $smarthost is the
  118. SMTP server to which you wish to connect including port in host:port format.
  119.  
  120. =head1 AUTHOR AND COPYRIGHT
  121.  
  122. Orignial code
  123. Net::SMTP::Server / SMTP::Server is Copyright(C) 1999,
  124.   MacGyver (aka Habeeb J. Dihu) <macgyver@tos.net>.  ALL RIGHTS RESERVED.
  125.  
  126. Modifications to Net::SMTP::Server::Relay
  127.   Ian R. Justman <ianj@esper.net>
  128.  
  129. You may distribute this package under the terms of either the GNU
  130. General Public License or the Artistic License, as specified in the
  131. Perl README file.
  132.  
  133. =head1 SEE ALSO
  134.  
  135. Net::SMTP::Server::Server, Net::SMTP::Server::Client,
  136. Net::SMTP::Server::Relay
  137.  
  138. =cut

Raw Paste


Login or Register to edit or fork this paste. It's free.