Solved

CGI form to e-mail problem

Posted on 2003-12-09
3
280 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 80 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

627 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