使用SMTP/PHP的间歇性联系人表单故障
我有一个使用php的联系人表单。它会间歇性地失败,并出现以下错误: 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。 请与服务器管理员联系,webmaster@website.com并告知他们错误发生的时间,以及您可能采取的任何可能导致错误的措施。 有关此错误的详细信息,请参阅服务器错误日志。 此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误 将SMTPDebug设置为3不会显示故障前的任何信息 查看日志文件,我看到: 脚本头过早结束 对等方重置连接:mod_fcgid:从FastCGI读取数据时出错 PHP版本目前是5.6,虽然我改为7.0,但没有什么不同。我从FastCGI切换到CGI,这也没什么区别 php表单如下所示:使用SMTP/PHP的间歇性联系人表单故障,php,Php,我有一个使用php的联系人表单。它会间歇性地失败,并出现以下错误: 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。 请与服务器管理员联系,webmaster@website.com并告知他们错误发生的时间,以及您可能采取的任何可能导致错误的措施。 有关此错误的详细信息,请参阅服务器错误日志。 此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误 将SMTPDebug设置为3不会显示故障前的任何信息 查看日志文件,我看到: 脚本头过早结束 对等方重置连接:m
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require '/home/username/phpmailer/vendor/autoload.php';
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp-mail.outlook.com"; //Gmail SMTP server address
$mail->Port = 587; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "myusername@outlook.com"; // Your full Gmail address
$mail->Password = "mypassword"; // Your Gmail password
// Compose
$mail->setFrom('myusername@outlook.com', 'Mailer');
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("myusername@outlook.com", "My Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? '<div class="alert alert-success" role="alert"><strong>Thank you for contacting me.</strong></div>' : '<div class="alert alert-danger" role="alert"><strong>Error!</strong>There was a problem delivering the message.</div>';
unset($mail);
}
?>
当它失败时,它可能(不总是)会在紧接着的下一次尝试中起作用。我看不出失败的模式
我读过关于mod_security是编辑.htaccess的原因或原因的评论,但我还处于未知状态。我不想做会造成安全漏洞的事情
非常感谢您的帮助。通常在调试尝试之前出现错误时会发生这种情况。在这种情况下,您可能不需要使用那些
use
语句。谢谢您的回复。主机服务器支持提供的示例中提供了Use命令。调试会在联系人表单发送成功时生成信息,但在发送失败时不会生成信息,这表明脚本根本没有运行。当您使用composer时,需要将use
。即使如此,我仍然严重怀疑名称空间是否正确。另外,phpmailer也会抛出异常,我在代码中没有看到任何try-catch块,如果出现错误,它可以捕获异常。我很困惑,这可能是因为我在这里缺乏知识。我使用composer安装phpmailer(我使用共享主机)。如果名称空间不正确,为什么它有时可以工作,而其他的却不行?如果在失败时没有看到调试信息(当它打开时),这难道不告诉我脚本根本没有被执行吗?老实说,我很久没有使用共享托管环境了。(当我使用共享主机时,Composer不在)。检查PHPMailer的代码时,我发现我使用了旧版本(5.2.25),因此我对名称空间的认识不正确。我对此表示歉意。但是异常问题仍然存在,您需要用try-catch
块封装setFrom
和Send
方法,因为它们可能会引发异常