XAMPP本地环境下PHPMailer联系表单无法发送邮件及类型未定义问题求助
XAMPP本地环境下PHPMailer联系表单无法发送邮件及类型未定义问题求助
各位开发者好,我正在搭建一个用户提交后能给我发送邮件的联系表单,因为用的是XAMPP本地环境,所以选择了PHPMailer来实现。我已经安装了Composer和PHPMailer,也反复核对过文件路径,但还是遇到了卡壳的问题,想请大家帮忙排查下。
我的PHP文件头部的引入代码如下:
<?php session_start(); require "C:/xampp/htdocs/Composer/vendor/autoload.php"; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/PHPMailer.php"; require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/SMTP.php"; require "C:/xampp/htdocs/Composer/vendor/phpmailer/phpmailer/src/Exception.php"; ?>
处理邮件发送的核心代码是这样的:
<?php $message = ""; if(isset($_POST["btn"])) { $userName = $_POST["username"]; $userPhone = $_POST["userphone"]; $userEmail = $_POST["useremail"]; $userSubject = $_POST["usersubject"]; $userMessage = $_POST["usermessage"]; $toemail = "myemail"; $mail = new PHPMailer(true); try { $mail -> isSMTP(); $mail -> Host = 'smtp.gmail.com'; $mail-> SMTPAuth = true; $mail-> Username = 'myemail'; $mail-> Password = 'mypassword'; $mail-> SMTPSecure = 'tls'; $mail-> Port = 587; $mail -> setFrom($userEmail, $userName); $mail -> addAddress($toemail); $mail -> isHTML(false); $mail -> Subject = $userSubject; $mail -> Body = "Name: $userName\r\n" . "Phone: $userPhone\r\n" . "Message: $userMessage\r\n"; $mail -> send(); $message = "Your information has been received"; } catch(Exception $e) { $message = "Error sending email. Please try again later"; } } ?>
目前遇到的具体问题:
- 提交表单后总是触发catch块的错误提示,说明try块内的邮件发送逻辑没有执行成功
- 编辑器的Intelephense工具提示P1009错误:
Undefined type PHPMailer\PHPMailer\PHPMailer(对应$mail = new PHPMailer(true);这一行),同时还有Undefined type PHPMailer\PHPMailer\Exception(对应catch(Exception $e)这一行)
我已经反复确认过文件路径是正确的,也试过AI给出的路径排查建议,但问题依旧存在。有没有大佬能指点下可能的原因?
备注:内容来源于stack exchange,提问作者Shady Sheen
相关产品推荐
相关产品推荐

