Solved

CGI form to e-mail problem

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

828 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