Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CGI form to e-mail problem

Posted on 2003-12-09
3
Medium Priority
?
281 Views
Last Modified: 2013-12-25
Hi,

I'm trying to create a perl script that will send an e-mail to me based on entries on an HTML form on my web page. However, I can't get it working, and get no useful message from my web-hosting company- a page is displayed that just says 'there is an error with your script'.
However, if I take out one line - close(MAIL); - the script executes printing the following:

Mail sent

Recipient names must be specified

However, the e-mail apears not to have been sent, as it doesn't arrive. Don't know why the 'Recipient names must be specified' message appears. I'd be grateful if anybody could help me with this. The code is below:

#!/usr/bin/perl -w
use strict;
use CGI qw(:all);
use CGI::Carp   qw(fatalsToBrowser);

  # Function for sending mail with an MTA like sendmail
  sub send_mail {
      my($to, $from, $subject, @body)=@_;
            my $sendmail="/usr/sbin/sendmail -oi -odq";
            open(MAIL, "|$sendmail") || die "Can't start sendmail: $!";
            print MAIL<<END_OF_HEADER;
From: $from
To: $to
Subject: $subject
END_OF_HEADER
foreach (@body) {
              print MAIL "$_\n";
                 }
           close(MAIL);
 }

print header;
my $return = param("return-addr");
if (! defined $return or ! $return) {
      print "You must supply an e-mail address<p>";
      exit;
}

my $subject = param("subject");
if (! defined $subject or ! $subject) {
      print "You must supply a subject<p>";
      exit;
}

send_mail('myname\@mydomain.net',
      $return,
      $subject,
            param("body"));

print "<p>Mail sent</p>";
0
Comment
Question by:million1977
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 9926265
Your call to sendmail should be

/usr/sbin/sendmail -oi -odq -t

Have you tried using FormMail from http://nms-cgi.sourceforge.net/
0
 

Author Comment

by:million1977
ID: 9927752
Sorry,
-t should have been in there, I took it out for debugging purposes.

I've fixed the program, but still don't know why the code above didn't work.
The problem was here:

print MAIL<<END_OF_HEADER;
From: $from
To: $to
Subject: $subject
END_OF_HEADER

I changed this to:

      print MAIL "From: $from\n";
      print MAIL "To: $to\n";
      print MAIL "Subject: $subject\n";

There was no whitespace in inappropriate places in the original (which was copied direct from a textbook), so I don't know what the problem was. I'm new to Perl- any of you Perl experts know what the problem might have been?

Thanks
0
 
LVL 48

Accepted Solution

by:
Tintin earned 240 total points
ID: 9959881
You need to separate the Subject header from the body of the email, ie:

print MAIL<<END_OF_HEADER;
From: $from
To: $to
Subject: $subject

END_OF_HEADER
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question