Solved

CGI form to e-mail problem

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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

16 Experts available now in Live!

Get 1:1 Help Now