Good day Bros. I needed a simple mail reflector and did not need all the functionality of listerv or majordomo, et al. So I wrote a script for this purpose myself. It is working fine as far as anticipated functions. Here is the code:
#!/usr/bin/perl -w use strict; use Mail::POP3Client; use Mail::Mailer; our $junk; my $pophost = ''; my $popuser = ''; my $poppw = ''; my $reflectoraddr = ''; my $subjprefix = ''; my $errorfrom = ''; my $errorbcc = ''; my %participants; open(IN,"participants.txt") or die "Can't open participants.txt: $!\n" +; while(<IN>) { chop; if ($_) { $participants{$_} = 1; } } close IN; my $pop = Mail::POP3Client->new(HOST => $pophost, USER => $popuser, PA +SSWORD => $poppw); our $mailer = new Mail::Mailer; my $msgcount = $pop->Count(); print "$msgcount messages\n"; foreach my $k (1..1) { my %header; my $head = $pop->Head($k); my @lines = split(/\n/,$head); my $headerinfo; my ($key,$value); for my $i (0..$#lines) { $lines[$i] =~ s/\r/ /g; if (substr($lines[$i],0,1) =~ /[A-Z]/) { if ($lines[$i] =~ /^Date/) { $key = "Date"; ($value = $lines[$i]) =~ s/Date: //; } else { ($key,$value) = split(/\:/,$lines[$i]); } $header{$key} = $value; } else { $lines[$i] =~ s/^\s+//g; $header{$key} .= " $lines[$i]"; } } if ($header{'Subject'} !~ /undeliverable|delivery (failure|error)/ +i) { delete($header{'Received'}); delete($header{'Envelope-to'}); $header{'Reply-to'} = $reflectoraddr; delete($header{'To'}); $header{'Subject'} =~ s/\[Crawdad\] //g; my $body = $pop->Body($k); open (OUT,">bodytext.txt") or die "No output: $!\n"; print OUT $body; close OUT; my $from; if ($header{'From'} =~ /\<.+\>/) { ($junk, $from) = split(/[\<\>]/,$header{'From'}); } else { $from = $header{'From'}; } if (defined($participants{$from})) { $header{'Subject'} = "\[$subjprefix\] ".$header{'Subject'} +; foreach my $p (keys %participants) { unless ($header{'From'} =~ /$p/) { print "Sending to $p\n"; $header{'To'} = $p; $mailer->open(\%header); print $mailer $body; $mailer->close; } } } else { print "Error to $from\n"; errormsg(\%header,$from); } } $pop->Delete($k); } $pop->Close(); sub errormsg { my ($header,$from) = @_; my $errormsg = "The address $from is not authorized to send to thi +s discussion list.\n\nIf you are a registered user, it may be that yo +u are sending from an e-mail account other than the one you normally +use. Please contact the discussion host to have this account added." +; delete($$header{'Content-Type'}); $$header{'Subject'} = 'ERROR: '.$$header{'Subject'}; $$header{'From'} = $errorfrom; $$header{'To'} = $from; $mailer->open($header); print $mailer $errormsg; $mailer->close; return; }
Since this is a reflector that will run on a chron job or something like that, I am concerned about the possibility of this thing getting error e-mails (e.g. if an address fails, an account gets suspended, etc.) and getting in a mail loop situation via sub errormsg. I have tried to prevent this with if ($header{'Subject'} !~ /undeliverable|delivery (failure|error)/i) but I'm not sure if this is sufficient or the best way to do it.

Any Mail Monks out there who can comment on whether this is good enough? Is there some kind of header I could look at to detect failures, rather than depending on the subject? I've looked around in the SMTP docs and I haven't seen anything that looks suitable.

Thanks.......Steve


In reply to Avoiding mail loops by cormanaz

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.