简体   繁体   中英

Perl script getting 500 Internal Server error, end of script output before headers

I have this script to send email. Everything works except that after the email is sent I receive the error below. It has to be something simple I am missing. I checked and cgi file is 755 and since it gets to the sub and executes its got to be coder error. Any help greatly appreciated.

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

The error in the server log is:

[Tue Jan 17 16:00:23.475272 2023] [cgi:error] [pid 230679] [client 69.90.223.10:35014] End of script output before headers: test3.cgi

Here is the code I am using, Perl CGI on Linux

#!/usr/bin/perl -Tw
# use warnings;
use strict;
use Net::SMTP;

send_mail('mail.xxxxxxx.com', # Host
 'order@xxxxxxxxx.com', #From
 'yyyyyyy@gmail.com', #to
 'Just a test, from mail.xxxxxx.com please ignore',  #Message body
 "Testing mail server email.\n" # Subject
 );
exit;

sub send_mail {
 my ($SMTP_HOST, $from, $to_addr, $body, $subject, $msg) = @_;

 $msg = "MIME-Version: 1.0\n"
 . "From: $from\n"
 . "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
 . "Subject: $subject\n\n"  # Double \n
 . $body;

 #
 # Open a SMTP session
 #
 my $smtp = Net::SMTP->new( $SMTP_HOST,
 Debug => 0,       # Change to a 1 to turn on debug messages
 Port => 587,
 );

 die("SMTP ERROR: Unable to open smtp session.\n")
 if(!defined($smtp) || !($smtp));

 die("Failed to set FROM address\n")
 if (! ($smtp->mail( $from ) ) );

 die("Failed to set receipient\n")
 if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_addr ) ) ) );

 $smtp->data( $msg );

 $smtp->quit;
 
}

Checked File attributes they are 755 Since it ran the code an performed the send email the Char set should be correct Being new to Perl not sure what else to check

This isn't a CGI program. You send no response for the request. Since there is no response (not even an invalid one), the script exits with nothing sent to standard output. The server realizes this is a problem, creates the 500 server error response, and adds to the error log that the script output ended before headers, which is the first thing the server expected to see.

You might want to start with a basic CGI tutorial to see how CGI works.

But, also realize that sending mail through a CGI program is a very 1990s thing to do. We don't typically do that anymore because we all figured out that letting anyone trigger mail through a public server was a bad idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM