OT: replacement for sendmail FEATURE(`redirect')
Ken A
ka at PACIFIC.NET
Fri May 27 23:20:22 IST 2005
[ The following text is in the "ISO-8859-1" character set. ]
[ Your display is set for the "US-ASCII" character set. ]
[ Some characters may be displayed incorrectly. ]
Is there something here to prevent mail loops?
If not, you might want to add something to do that.
Nothing like 2 poorly configured autoresponders!
Ken A
Pacific.Net
Jeff A. Earickson wrote:
> Gang,
>
> If any of you use sendmail's REDIRECT feature to bounce "User has moved"
> messages back for closed accounts, then you probably know that the feature
> is lame beyond words. It returns a message that is inscrutable to
> everybody
> but mail gurus. While I've used the feature at my site for a while, most
> people hate it because of the returned messages.
>
> I wrote a program mailer "redirect.pl" in perl (attached) and a little
> converter program for your aliases file (attached) to convert the old
> style REDIRECTs to my program mailer.
>
> I have redirect.pl in production at my site now and people are much
> happier. The perl script also offers a "-f" option to forward the
> message on to the new address, as well as return a redirect message
> back to the sender. While the forward option works, it is hit-or-miss
> with attachments.
>
> If you want to see how it works in real life, please send email to
> trobbins at colby.edu. This address will not forward messages.
>
> I offer this tool to my fellow MailScanner users in the hope you find it
> useful. Fixes, suggestions, info on use with other mailers (eg Postfix)
> would be appreciated.
>
> Jeff Earickson
> Colby College
>
> ------------------------ MailScanner list ------------------------
> To unsubscribe, email jiscmail at jiscmail.ac.uk with the words:
> 'leave mailscanner' in the body of the email.
> Before posting, read the Wiki (http://wiki.mailscanner.info/) and
> the archives (http://www.jiscmail.ac.uk/lists/mailscanner.html).
>
> Support MailScanner development - buy the book off the website!
>
>
> ------------------------------------------------------------------------
>
> #!/usr/bin/perl
> #
> #---returns a nice looking customized redirect message
> #---instead of the ugly "Subject: Returned mail" that sendmail returns
> #
> #---Usage:
> #
> # For a user, put the following in your aliases file:
> #
> # joeblow:"| redirect.pl [-df] joeblow new_address at somewhere.com"
> #
> # where "joeblow" is the old account at your site, "new_address at somewhere.com"
> # is their address to advertise at their new site, and redirect.pl is this
> # script. If you use smrsh with sendmail, you will have to modify the contents
> # of your /var/adm/sm.bin directory. "man smrsh" for details.
> #
> # The optional flags are:
> # -d for debug mode (write debug output to a file in /tmp),
> # -f for forward the email to the new address as well as send the
> # redirect message back to the sender (forward+redirect)
> #
> # Note: does not write to stdout, just has different exit codes
> # forwarding attachments is also dicey.
> #
> # Written by Jeff Earickson, Colby College (jaearick at colby.edu) 5/27/05
>
> #---see search.cpan.org for these modules
> use Getopt::Std; # for command line parsing
> use Mail::MboxParser; # parses the mailbox
> use Mail::Sender; # SMTP connectivity to resend email
>
> #---parse the command line
> getopts('df') or exit 251;
>
> ###########################################################
> #---stuff you will need to customize for your site
> #---also see the msgbody here-is document below
> ###########################################################
>
> #---your domain name
> $mydomain = "colby.edu";
>
> #---your SMTP server
> $mysmtp = "colby.edu";
>
> #---who the redirect message should come from, eg postmaster
> $postmaster = "postmaster\@$mydomain";
>
> #---Subject: line for the redirect message
> $redirect_subject = "Colby user $ARGV[0]\@$mydomain has moved";
>
> ###########################################################
>
> #---open a debug file if needed
> if($opt_d)
> {
> open(DEBUG,"> /tmp/redirect.debug") or exit 252;
> print DEBUG "DEBUG=== redirect.pl: @ARGV\n";
> if($opt_f)
> {
> print DEBUG "DEBUG=== forwarding flag turned on\n";
> }
> print DEBUG "\nDEBUG=== The email message is:\n";
> }
>
> #---open a temporary file to store the message in
> #---and stuff the email message coming from stdin into the file
> open(MBOX,"> /tmp/redirect.mbox.$$") or exit 253;
> while(<STDIN>)
> {
> if($opt_d)
> {
> print DEBUG $_;
> }
> print MBOX $_;
> }
> close(MBOX);
>
> #---options set for Mail::MboxParser
> #---caching not used
> my $parseropts = {
> enable_cache => 0,
> enable_grep => 1,
> };
>
> #---parse the temporary file
> my $mb = Mail::MboxParser->new("/tmp/redirect.mbox.$$",
> decode => 'NEVER',
> parseropts => $parseropts);
>
> #---process every msg in the mailbox, should be one msg
> $count=0;
> for my $msg ($mb->get_messages)
> {
> $count++;
> $orig_subject = $msg->header->{subject};
> $orig_from = $msg->header->{from};
> $orig_to = $msg->header->{to};
> $orig_body = $msg->body;
>
> #---debug mode: tell will would happen
> if($opt_d)
> {
> print DEBUG "\nDEBUG=== Parsing the Message...\n";
> print DEBUG "From: ",$orig_from,"\n";
> print DEBUG "To: ", $orig_to,"\n";
> print DEBUG "Subject: ",$orig_subject,"\n";
> print DEBUG "\nDEBUG=== The Message Body\n";
> print DEBUG $orig_body;
> }
> }
>
> if($opt_d)
> {
> print DEBUG "\nProcessed $count messages in mailbox\n";
> }
>
> ###########################################################
> #---this is the body of the message that gets sent back
>
> $redirect_msg = << "EOF";
> Hi,
> The person using the e-mail address \"$ARGV[0]\@colby.edu\" has
> left the College for good. Before leaving, $ARGV[0] wanted you
> to know that his/her new e-mail address is:
>
> $ARGV[1]
>
> If you are writing to $ARGV[0] about Colby business-related issues,
> then please consult Colby's online personnel directory at:
>
> http://www.colby.edu/directory_cs/index.cfm
>
> to find the correct department or person to write to.
>
> EOF
>
> #----tack on a "we also delivered it" if forward option set
> if($opt_f)
> {
> $redirect_msg .= << "EOF2"
> Please note the new address above. Colby's mail server has also
> forwarded you message to $ARGV[1] as a courtesy.
> ==> However, do NOT count on this happening in the future. <==
> If your message had attachments, they may or may not be useable
> by the recipient. You should retransmit directly to the address
> above. Good luck.
>
> Thank You,
> Colby Postmaster
> EOF2
> }
> #---otherwise tell the sender that msg was not deleivered.
> else
> {
> $redirect_msg .= << "EOF3"
> Please note the new address above, and then resend your message
> (if appropriate) with the subject:
>
> $orig_subject
>
> to the new e-mail address. YOUR MESSAGE WAS NOT DELIVERED.
>
> Thank You,
> Colby Postmaster
> EOF3
> }
>
> ###########################################################
>
> #---compose the redirect message and send it
> if(ref((new Mail::Sender)->MailMsg( {
> to => $orig_from,
> from => $postmaster,
> subject => $redirect_subject,
> smtp => $mysmtp,
> msg => $redirect_msg } ) ) )
> {
> $retcode = 0;
> }
> else
> {
> $retcode = 254;
> }
>
> #---if we are going to forward the message too, then do it
> if($opt_f)
> {
> $forward_subject = $orig_subject . " (forwarded)";
>
> if($opt_d)
> {
> print DEBUG "\nDEBUG=== What the forward will look like...\n";
> print DEBUG "From: ",$orig_from,"\n";
> print DEBUG "To: ",$ARGV[1],"\n";
> print DEBUG "Subject: ",$forward_subject,"\n";
> print DEBUG $orig_body;
> print DEBUG "===\n";
> }
>
> if(ref((new Mail::Sender)->MailMsg( {
> to => $ARGV[1],
> from => $orig_from,
> subject => $forward_subject,
> smtp => $mysmtp,
> msg => $orig_body } ) ) )
> {
> $retcode = 0;
> }
> else
> {
> $retcode = 255;
> }
> }
>
> if($opt_d)
> {
> close(DEBUG);
> }
> unlink("/tmp/redirect.mbox.$$");
> exit $retcode;
>
>
> ------------------------------------------------------------------------
>
> #!/usr/bin/perl
> #
> #---convert an alias file with old .REDIRECT style stuff
> #---to my redirect.pl format
> #
>
> open(IN,"< $ARGV[0]") or die "cannot open $ARGV[0]: $!";
> open(OUT,"> $ARGV[1]") or die "cannot open $ARGV[1]: $!";
>
> while(<IN>)
> {
> chomp;
>
> if( $_ =~ /^(\S+):(\S+).REDIRECT$/)
> {
> print OUT "$1:\"| redirect.pl $1 $2\"\n";
> }
> else
> {
> print OUT "$_\n";
> }
> }
> close(IN);
> close(OUT);
------------------------ MailScanner list ------------------------
To unsubscribe, email jiscmail at jiscmail.ac.uk with the words:
'leave mailscanner' in the body of the email.
Before posting, read the Wiki (http://wiki.mailscanner.info/) and
the archives (http://www.jiscmail.ac.uk/lists/mailscanner.html).
Support MailScanner development - buy the book off the website!
More information about the MailScanner
mailing list