#!/usr/bin/perl use strict; # to catch stupid errors use Getopt::Long; use Pod::Usage; # ---------- End of Configuration ----------------------------------- # Extract the command line arguments # header to be inserted # --dups: to allow duplicate headers # GetOptions ( 'dups!' => \(my $dups = ''), 'help' => \(my $help = ''), ) or pod2usage(2); # Print help text and exit if requested. pod2usage(1) if $help; # Fetch the header if available pod2usage(2) if (@ARGV != 1); # # Try to split the header into parts # my $header = shift @ARGV; my ($headerName, $headerContents) = split /:/, $header; # # Quit now if the header looks screwy. # pod2usage(2) if (!defined($headerName) || !defined($headerContents)) ; # # Read stdin, adding the training header just before the message body. # my $headerAdded = 0; while (defined(my $thisLine = )) { # Drop existing headers. if ($dups || !($thisLine =~ m/^$headerName/)) { if (!$headerAdded && ($thisLine =~ m/^$/)) { $headerAdded = 1; print "$header\n"; }; print $thisLine; } } __END__ =head1 NAME addHeader - Add a header to a mail message =head1 SYNOPSIS addHeader [--dups] header =head1 OPTIONS =over 8 =item B<--dups> Allow duplicate headers =item B<--nodups> Do not allow duplicate headers. The specified header replaces the one(s) found in the message. (Default) =item B<--help> Print this text =back =cut