PHPMailer脚本运行出现500内部服务器错误求助
问题背景
运行PHPMailer脚本时持续出现500内部服务器错误,已检查语法、文件路径,通过Composer重装PHPMailer并调整命名空间位置后问题仍未解决,使用Knownhost主机,服务商确认服务器及端口正常。
原脚本代码:
<?php include("../lib/PHPMailer/src/PHPMailer.php"); //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\SMTP; use PHPMailer\PHPMailer\Exception; $email = "test@test.com"; $username = "test"; //Create an instance; passing `true` enables exceptions $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = 'mail.*****.com'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = '*****@*****.com'; //SMTP username $mail->Password = '*****'; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail->setFrom('noreply@*****.com', 'Mailer'); $mail->addAddress($email, $username); //Add a recipient $mail->addAddress('ellen@example.com'); //Name is optional $mail->addReplyTo('noreply@*****.com', 'Information'); //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = 'DestroyMyApp Registration Validation'; $mail->Body = 'Test'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } ?>
修改后的脚本头部:
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require '../vendor/phpmailer/phpmailer/src/Exception.php'; require '../vendor/phpmailer/phpmailer/src/PHPMailer.php'; require '../vendor/phpmailer/phpmailer/src/SMTP.php'; require '../vendor/autoload.php'; //Create an instance; passing `true` enables exceptions $mail = new PHPMailer(true); try { //Server settings ....
排查解决步骤
获取具体错误信息:500是通用服务器错误,看不到细节。在脚本最顶部添加以下代码强制显示错误:
error_reporting(E_ALL); ini_set('display_errors', 1);或者直接查看Knownhost主机的服务器错误日志(比如cPanel中的"Error Log"工具),日志会明确指出是语法错误、类未找到还是权限问题。
修复Composer自动加载冲突:修改后的脚本同时手动引入三个PHPMailer文件和
autoload.php,会导致类重复定义引发致命错误。只保留autoload.php的引入即可,删除另外三个require:<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require '../vendor/autoload.php'; //Create an instance; passing `true` enables exceptions $mail = new PHPMailer(true); //...后续代码检查文件权限:确认
vendor目录及其下所有文件的权限正确,PHP运行用户需要读取权限。一般目录权限设为755,文件权限设为644,不要用777(有安全风险且可能触发服务器限制)。验证PHP版本兼容性:PHPMailer 6.x要求PHP 5.5及以上版本,通过
phpinfo()查看Knownhost主机的PHP版本。如果服务器用的是PHP 5.4及以下,要么升级PHP,要么降级PHPMailer到5.x版本。检查必要PHP扩展:PHPMailer发送加密邮件需要
openssl扩展支持,通过phpinfo()搜索"openssl"确认扩展是否启用。未启用的话联系主机商开启。测试基础PHP环境:先运行一个最简PHP脚本(比如
<?php echo "Hello World"; ?>),确认服务器能正常解析PHP,排除服务器本身的PHP配置问题。调整SMTP配置测试:尝试更换加密方式和端口,比如改用:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587;同时临时关闭
SMTPDebug(设为0),避免Debug输出干扰响应头。
内容的提问来源于stack exchange,提问作者bobafart

