Solved

CGI form to e-mail problem

Posted on 2003-12-09
3
272 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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 dynamically set the form action using jQuery.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now