如何修复PHP致命错误yii\base\ErrorException:找不到Swift_Transport_EsmtpTransport类
Swift_Transport_EsmtpTransport类找不到的致命错误 问题场景
你在生产主机环境尝试发送邮件时触发了PHP致命错误,错误信息如下:
[yii\base\ErrorException] Class 'Swift_Transport_EsmtpTransport' not found
错误出现在文件/home/u536070/operettalife.ru/vendor/swiftmailer/swiftmailer/lib/classes/Swift/SmtpTransport.php第23行,调用栈指向yii\base\ErrorHandler::handleFatalError(),相关代码片段:
* @author Chris Corbyn * * @method Swift_SmtpTransport setUsername(string $username) Set the username to authenticate with. * @method string getUsername() Get the username to authenticate with. * @method Swift_SmtpTransport setPassword(string $password) Set the password to authenticate with. * @method string getPassword() Get the password to authenticate with. * @method Swift_SmtpTransport setAuthMode(string $mode) Set the auth mode to use to authenticate. * @method string getAuthMode() Get the auth mode to use to authenticate. */ class Swift_SmtpTransport extends Swift_Transport_EsmtpTransport { /** * Create a new SmtpTransport, optionally with $host, $port and $security. * * @param string $host * @param int $port * @param string $security */ public function __construct($host = 'localhost', $port = 25, $security = null)
而本地服务器运行完全正常,这种环境差异导致的类缺失问题,通常可以通过以下方法修复:
修复方法
1. 重新完整安装Composer依赖
生产环境的依赖包很可能因为安装中断、上传遗漏或者缓存问题导致文件不完整。执行以下命令重新安装依赖,确保所有文件都正确加载:
composer install --no-dev --optimize-autoloader
--no-dev:跳过开发环境依赖,适合生产环境--optimize-autoloader:优化自动加载器,生成更高效的类映射,避免类路径查找错误
2. 检查文件完整性与权限
手动确认生产环境中vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php文件是否存在。如果文件缺失,说明依赖安装不完整或者上传过程中丢失了文件。
同时,确保Web服务器进程对vendor目录有读取权限,避免因为权限不足无法加载类文件:
# 调整目录权限,确保可读 chmod -R 755 /home/u536070/operettalife.ru/vendor # 调整目录所属用户组,匹配你的Web服务器用户(比如www-data、nginx) chown -R www-data:www-data /home/u536070/operettalife.ru/vendor
3. 重新生成自动加载映射
如果依赖文件存在但仍然找不到类,可能是自动加载映射文件过期。执行以下命令重新生成:
composer dump-autoload
这个命令会更新vendor/autoload.php和相关的类映射文件,确保Swiftmailer的类能被正确识别。
4. 确认Swiftmailer版本兼容性
检查本地和生产环境的Swiftmailer版本是否一致,版本差异可能导致类名或结构变化:
composer show swiftmailer/swiftmailer
如果版本不一致,更新生产环境的依赖到与本地相同的版本,或者根据Yii2框架的要求调整composer.json中的版本约束后重新安装。
内容的提问来源于stack exchange,提问作者AngeliqueV

