user1899817
user1899817

Reputation: 89

Sendemail using perl in html form doesn't work

I am trying to create a html form to send email with issues. My sendmail.pl works with undefined hash variables when I run the perl script individually but not when I click send email from html form. Instead of sending email it just downloads the file in the browser. Can someone please help me why it doesn't email from html form? Should I save the script in a specific location?

<form action="sendmail.pl" method="post" class="incident-change-form">
    <div class="row">
        <div class="col span-1-of-3">
            <label for="Servers">Server</label>
        </div>
        <div class="col span-2-of-3">
            <select name="Servers" id="Servers">
                <option value="Server A">Server A</option>
                <option value="Server B" selected>Server B</option>
                <option value="Server C">Server C</option>
            </select>
        </div>
    </div>
        <div class="row">
        <div class="col span-1-of-3">
            <label for="Incident">Incident Numbers</label>
        </div>
        <div class="col span-2-of-3">
            <input type="text" name="Incident" id="Incident" placeholder="INCxxxxx" required>
        </div>
    </div>
    <div class="row">
        <div class="col span-1-of-3">
            <label for="Message">Issue Details</label>
        </div>
        <div class="col span-2-of-3">
            <textarea name="Message" id="Message" placeholder="Enter the incident details" required></textarea>
        </div>
    </div>
    <div class="row">
        <div class="col span-1-of-3">
            <label>&nbsp;</label>
        </div>
        <div class="col span-1-of-3">
            <input type="submit" value="Send Email">
        </div>
    </div>
</form>

CGI perl script

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;

use Data::Dumper;
use CGI;
my $q = new CGI;

 my %data;
 $data{Servers} = $q->param('Servers');
 $data{Incident} = $q->param('Incident');
 $data{Message} = $q->param('Message');

 print $q->header;

# print "response " . Dumper \%data;
 print Dumper( \ %data);

$ENV{PATH} = '';
sendmail(
    'Target <email@address.com>',
    'Daily Checks',
    'submitted: ' . Dumper(\%data),
    'Source <email@address.com>');

sub sendmail {
    my ($tofield, $subject, $text, $fromfield) = @_;
    my $mailprog = "/usr/sbin/sendmail";

    open my $ph, '|-', "$mailprog -t -oi" or die $!;
    print $ph "To: $tofield\n";
    print $ph "From: $fromfield\n";
    print $ph "Reply-To: $fromfield\n";
    print $ph "Subject: $subject\n";
    print $ph "\n";
    print $ph "$text";
    close $ph;
    return ;
}

Upvotes: 1

Views: 181

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

It's hard to be sure, but this problem has nothing to do with the Perl code (as you can tell because it works outside of the CGI environment).

It's about how your web server is configured. And I know nothing about that, so this is all guesswork.

It looks like you have your CGI program in a file called sendmail.pl in the same directory as your HTML file. That's a bit strange. Generally, web servers are configured to recognise CGI programs in one of two ways:

  • By having the extension .cgi
  • By being in a special directory (which is often called cgi-bin)

As your CGI program doesn't fulfil either of those criteria, it is not being recognised as a CGI program and the web server is just serving the file as content.

As I said before, this is guesswork, but you could start by renaming the file to sendmail.cgi (and, of course, changing the reference to it in your HTML file). If that doesn't work, try looking for a cgi-bin directory on the server and moving the file to there (again, you'll need to change the path in your HTML file).

If all else fails, try finding someone who knows how your web server is configured and asking them how you run a CGI program.

Upvotes: 2

Related Questions