Have a look at Net::SMTP::Server.

<update>

It's really straight forward...

#!/usr/bin/perl -w use strict; # $Id: mta.pl,v 0.0 2007/01/19 19:38:39 shmem Exp $ # local delaying MTA use Carp; use File::Spec; use Net::SMTP; use Net::SMTP::Server; use Net::SMTP::Server::Client; my $relay = 'smarthost'; my $port = 25; my $time = time; my $spooldir = 'delayed'; my $maxmsgs = 30; my $timeframe = 3600; -d $spooldir or mkdir $spooldir,0755 or die "spooldir: $!\n"; my $server = new Net::SMTP::Server('localhost', $port) || croak("Unable to handle client connection: $!\n"); my $sentcount = 0; while(my $conn = $server->accept()) { # receive mail and spool it. my $client = new Net::SMTP::Server::Client($conn) || croak("Unable to handle client connection: $!\n"); my $file = File::Spec->catfile($spooldir,time); open O, '>', $file or die "Can't write $file: $!\n"; if ( $client->process ) { print O $client->{'FROM'}.$/; print O join('|',@{$client->{'TO'}}).$/; print O $client->{'MSG'}.$/; close O or die "Can't close filehandle O: $!\n"; warn "[$file] message queued.\n"; } # else { *shrug*. Client quit. } # deliver mails. opendir(D, $spooldir) or die "Can't read '$spooldir': $!\n"; my @files = grep (!/^\.\.?/,readdir D); foreach my $file (@files) { # reset $sentcount if timeframe passed if (time - $time >= $timeframe) { $sentcount = 0; $time = time; warn "timer reset.\n"; } if ($sentcount == $maxmsgs) { warn "Delivery delayed, reached $maxmsgs in $timeframe sec +s.\n"; last; } my $mailfile = File::Spec->catfile ($spooldir,$file); open MAIL, '<', $mailfile or die "Can't read '$mailfile': $!\n"; chomp(my $FROM = <MAIL>); chomp(my $TO = <MAIL>); my $MSG = do { local $/; <MAIL> }; $TO = [split /\|/, $TO]; my $sent = relay ($FROM, $TO, $MSG); if ($sent) { $sentcount++; unlink $mailfile or die "Can't unlink '$mailfile': $!\n"; warn "[$file]: (#$sentcount) sent\n"; } else { warn "[$file]: delivery failed \n"; } } } sub relay { my ($FROM, $TO, $MSG) = @_; my $smtp = new Net::SMTP($relay); $smtp -> mail ($FROM) or return; $smtp -> to (@{$TO}) or return; $smtp -> data ($MSG) or return; $smtp -> quit or return; }

Eliminating the flaw that mails are only sent after receiving one is left as an excercise to the reader ;-)

</update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: mail counter by shmem
in thread mail counter by former33t

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.