简体   繁体   English

Perl 脚本出现 500 内部服务器错误,脚本结束 output 在标头之前

[英]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.我有这个脚本来发送 email。一切正常,除了在发送 email 之后我收到以下错误。 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.我检查了一下,cgi 文件是 755,因为它到达了 sub 并执行了它必须是编码器错误。 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.此外,在尝试使用 ErrorDocument 处理请求时遇到了 500 Internal Server Error 错误。

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 [2023 年 1 月 17 日星期二 16:00:23.475272] [cgi:error] [pid 230679] [client 69.90.223.10:35014] 头文件之前的脚本 output 结束:test3.cgi

Here is the code I am using, Perl CGI on Linux这是我正在使用的代码,Linux 上的 Perl CGI

#!/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检查文件属性它们是 755 因为它运行代码并执行发送 email 字符集应该是正确的 Perl 的新手不知道还有什么要检查

This isn't a CGI program.这不是 CGI 程序。 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.由于没有响应(甚至没有无效响应),脚本退出时没有向标准 output 发送任何内容。服务器意识到这是一个问题,创建 500 服务器错误响应,并将脚本 output 之前结束的错误日志添加到错误日志中标头,这是服务器希望看到的第一件事。

You might want to start with a basic CGI tutorial to see how CGI works.您可能希望从基本的 CGI 教程开始,了解 CGI 的工作原理。

But, also realize that sending mail through a CGI program is a very 1990s thing to do.但是,也要意识到通过 CGI 程序发送邮件是 20 世纪 90 年代的事情。 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.我们通常不再这样做了,因为我们都认为让任何人通过公共服务器触发邮件是个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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