Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-06-2005, 02:43 PM   #1
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
Of Perl CGI scripts and Sendmail

Hey all. I'm working on a script to handle web forms, and email their contents to me via sendmail. I do not want to have to type the parameters out in the script itself, because that would make it terribly unflexible. But when I switched to having it just print every parameter and its value, it began having troubles printing variables after the Subject line.

The following works as expected:
#!/usr/bin/perl -w
use diagnostics;
use CGI qw(:standard);
print header(-type=>'text/html');
$$_ = param $_ for param();
my($_sendmail,$_to) = ('/usr/sbin/sendmail','user@domain.tld');
open MAIL, "| $_sendmail $_to" or die "Can't open sendmail: message = '$!'";
print MAIL "To: $_to\n";
print MAIL "From: Chrax\n";
print MAIL "Subject: ", param('Subject'), "\n";
print MAIL "Test\n\nChris\n";
close MAIL or die "Can't close message: message = '$!'";
$body .= "$_: $$_\n" for param();

print << "EOF";
Thank you for contacting us. You will now be redirected to <a href="/"></a>.<br><br>
Your message text was:<br>
... but if you switch
print MAIL "Subject: ", param('Subject'), "\n";
print MAIL "Test\n\nChris\n";
print MAIL "$_: $$_\n" for param();
then the email is sent with no contents, but still a subject line (Subject is the first parameter from the form.).

Now, I have tried creating a single scalar and printing that, but the same problem arises. The only time when the body is printed until the end is when there is no subject line. I have diagnostics and warnings on, so I know that the script is not exiting prematurely. And since the variables are later printed, it is not that they are not being initialized properly.

If anybody knows why this is screwing up and has any advice, I am completely stumped. I am using Perl 5.005_02, but since I'm using a server that I can only telnet into, that's what I'm stuck with.

Old 06-06-2005, 03:02 PM   #2
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
The final header needs to have 2 newlines after it and not one. You'll need to print another newline before starting the message body. Also note that what you are doing could allow users to pass arbirtrary headers to your e-mail.
Old 06-06-2005, 05:21 PM   #3
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
Thank you.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problems in running cgi/perl scripts... raymond Linux - General 17 02-09-2010 05:06 AM
Running perl scripts outside of cgi-bin ylikone Programming 1 09-08-2005 05:34 AM
Disabling Perl/CGI Scripts totally renegadeavenger Linux - General 1 08-14-2005 08:28 PM
learning to write Perl CGI scripts djgerbavore Programming 1 10-19-2004 01:31 PM
Running Perl/CGI scripts hso_sof Linux - Newbie 3 08-22-2003 11:26 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration