North323 has asked for the wisdom of the Perl Monks concerning the following question:

i have a basic perl script that creates an email and forwards it. i would like to add something to this script that will remove ALL headers from any email that it forwards. i am a super newb to perl so any help would be awesome. here is my script:
#!/usr/bin/perl use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); ######################## Program Header ############################## +########### ## ## Copyright (C) 2004 Poetic Pollution ## Script Name: BareBonesMailer ## Version: 0.2.4 ## URL: http://poeticpollution.net/scripts/BareBonesMailer/ ## ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License ## as published by the Free Software Foundation; either version 2 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## Please read the full GNU GPL: http://www.gnu.org/copyleft/gpl.html ## ###################################################################### +########### ######################## User Defined Variables ###################### +## ## Edit as needed... # Path to sendmail $mailProg = "/usr/sbin/sendmail -t"; # Your email address and be sure to leave the \ in front # of the @ or else you'll get errors.. ;) $toEmail = "you\@yourdomain.com"; # Domains that are able to use the mailer script # Enclose in quotes and seperate each with a comma @valid_referrers = ("yourdomain.com","yourotherdomain.com"); # Redirect them to this URL: # This should be a pre-existing file with a thank you # message or something similar.. $redirectURL = "http://www.yourdomain.com/thanks.html"; # Your time zone difference from GMT $offSet = "-7"; # Military time 0 = off 1 = on $milTime = 0; ######################## Engage Mailer ######################## ## Don't Edit Below Here Unless You Know How... $valid_http_referrer = 0; foreach(@valid_referrers) { if($ENV{'HTTP_REFERER'} =~ /^https?:\/\/(www\.)?$_/i) { $valid_http_referrer = 1; last; } } unless($valid_http_referrer) { &headerFooter; print qq|You are coming from the wrong page. Please go back and tr +y again..\n|; print @footer; exit; } $fromName = param('nameField'); $fromEmail = param('emailField'); $formContents = param('formField'); $formSubject = param('Subject'); unless($fromName) { &headerFooter; print qq|The <b>Name</b> field is required. Please go back and try + again..\n|; print @footer; exit; } unless(($fromEmail) && ($fromEmail =~ /.*\@.*/)) { &headerFooter; print qq|The <b>Email</b> field is required. Please go back and tr +y again..\n|; print @footer; exit; } unless($formContents) { &headerFooter; print qq|You must include a message of some sorts. Please go back +and try again..\n|; print @footer; exit; } @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); @time = gmtime(time + (3600 * $offSet)); $newMinSec = sprintf("%02d:%02d",$time[1],$time[0]); $newDay = sprintf("%02d",$time[3]); $years = $time[5] + 1900; if ($milTime) { $hours = sprintf("%02d",$time[2]); } else { if ($time[2] < 12) { if ($time[2] == 0) { $hours = 12; } else { $h +ours = $time[2]; } $amPm = " AM"; } else { if ($time[2] > 12) { $hours = $time[2] - 12; } else { $hour +s = $time[2]; } $amPm = " PM"; } } $dateFormat = "$newDay/$months[$time[4]]/$years at $hours:$newMinSec$a +mPm"; $remoteAddress = $ENV{'REMOTE_ADDR'}; $remoteHost = $ENV{'REMOTE_HOST'}; @subnetNumbers = split (/\./, $remoteAddress); $packedAddress = pack ("C4", @subnetNumbers); ($remoteHost) = gethostbyaddr ($packedAddress, 2); open(MAIL, "|$mailProg") || die "Unable to open Mail Program."; print MAIL "To: $toEmail\n"; print MAIL "From: $fromEmail\n"; print MAIL "Subject: $formSubject\n"; print MAIL "Date: $dateFormat.\n\n"; print MAIL "Name: $fromName <$fromEmail>\n"; print MAIL "$remoteHost / $remoteAddress\n"; print MAIL "Browser: $ENV{'HTTP_USER_AGENT'}\n"; print MAIL "Subject: $formSubject\n\n"; print MAIL "$formContents\n"; close(MAIL); print "Location: $redirectURL\n\n"; exit; sub headerFooter { # ------------------------------------------------ open (FILE, "mailer-template.html") || die "Unable to open the Mailer +Template for reading.."; @HTMLFile = <FILE>; close(FILE); for ($a = 0; $a < @HTMLFile; $a++) { if ($HTMLFile[$a] =~ /<!-- mailer + output will be here -->/) { $foundIt = 1; last; } } unless($foundIt) { die "HTML comments not found in the Mailer Template +.."; } for ($i = 0; $i < $a; $i++) { push(@header, $HTMLFile[$i]); } for ($i = $a; $i < @HTMLFile; $i++) { push(@footer, $HTMLFile[$i]); } print "Content-type: text/html\n\n"; print @header; }

Replies are listed 'Best First'.
Re: Removing Headers in Perl
by Corion (Patriarch) on Nov 11, 2007 at 20:34 UTC

    The big problem in your program is that you are accepting untrusted user input and pass that on to the sendmail program. You will need to make sure that the $fromEmail, $formSubject, $dateFormat, $fromName, $remoteHost, $remoteAddress, and the $ENV{'HTTP_USER_AGENT'] all are well-formed and especially do not contain newlines.

    Personally, I would reject anything that contains a character outside of [\x20-\x7f], but that might preclude people with accents in their name.

    Have you looked at the safer and well-maintained offerings from the NMS Posse ? They also have a form mailer, which might be what you need.

      thats great but how do i remove the headers from my emails when i sent them

        Once you've sent your email, it's too late to remove any headers.

        Maybe you don't want to print the headers in the first place?

        Maybe you can share with us the exact problem you are trying to solve?

        Maybe this is an XY Problem?

        In the transmission text, the headers are delimited from the mail body by two consecutive newline sequences (\r\n\r\n), so you could get creative with that if you save the output to a file instead of piping it to sendmail immediately.

Re: Removing Headers in Perl
by GrandFather (Saint) on Nov 11, 2007 at 20:53 UTC

    Perl philosophy says "laziness is a virtue". Often being lazy means doing a little bit of work up front to save a lot of work later. Two cases in point (and they are related): preview your nodes and use strictures. Both give you a heads up about nasty stuff down the track.

    In this case taint checking ought be added to the strictures recommendation.

    You may find that MIME::Lite or one of the many other email manipulating modules make editing headers (in particular) and managing email in general much easier.


    Perl is environmentally friendly - it saves trees
      this is for internal email only, the email does not hit the internet. just looking for a quick easy way to strip the email headers.
Re: Removing Headers in Perl
by LassiLantar (Monk) on Nov 11, 2007 at 19:34 UTC

    Hi North323,

    Please edit your post by surrounding the code in <code> tags. Check out Writeup Formatting Tips for info on formatting posts. It's nigh on impossible for us to give any help or advice if we can't read your code.

      thank you...my first post
Re: Removing Headers in Perl
by narainhere (Monk) on Nov 12, 2007 at 07:25 UTC
    If you are so strong about not using any other mailing modules suggested by others, then you might have to change your script.After receiving this line
    $formContents = param('formField');
    you should do some regex and remove the previous forward headers.If you could post the headers you currently receive in the mail I can suggest some Regex.But mind this:even after removing the previous headers, you cannot completely get rid of the mail header because the lines
    print MAIL "To: $toEmail\n"; print MAIL "From: $fromEmail\n"; print MAIL "Subject: $formSubject\n";
    by themselves become a header.

    The world is so big for any individual to conquer