PHP 无法识别 Elastic Beanstalk 中的邮件程序 class
PHP Mailer class in Elastic Beanstalk not recognised
根据我对 Elastic Beanstalk 的理解,如果您在根目录中包含一个 composer.json
文件并且不包含供应商文件,那么 composer 将根据 JSON 指令自动处理该库。
composer.json
文件是根据 PHPMailer Github 上的当前第 51 行 json 文件,它位于我的 EB 根文件夹中 /public
和 .ebextensions
然后我有这个 PHP 脚本 (public/phphead.php
),从 AWS 文档中复制
// 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;
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require '../vendor/autoload.php';
// requiring /vendor/autoload.php would throw a fatal error and so I presume this is where composer creates the vendor dir. no more fatal errors have been thrown and I have been looking to confirm this is the right location but I have only the lack of errors to make me think composer is found and working...
// ....
$mail = new PHPMailer(); //LINE 64 as per error log
EB 的错误日志输出:
PHP Fatal error: Uncaught Error: Class
'PHPMailer\PHPMailer\PHPMailer' not found in
/var/app/current/public/phpHead.php:64\nStack trace:\n#0
/var/app/current/public/index.php(3): include()\n#1 {main}\n thrown in
/var/app/current/public/phpHead.php on line 64
我正在使用的 composer.json
(与 PHPMailer 的 composer.json
文件相同,取自 GitHub):
{
"name": "phpmailer/phpmailer",
"type": "library",
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
"authors": [
{
"name": "Marcus Bointon",
"email": "phpmailer@synchromedia.co.uk"
},
{
"name": "Jim Jagielski",
"email": "jimjag@gmail.com"
},
{
"name": "Andy Prevost",
"email": "codeworxtech@users.sourceforge.net"
},
{
"name": "Brent R. Matzelle"
}
],
"require": {
"php": ">=5.5.0",
"ext-ctype": "*",
"ext-filter": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.2",
"phpunit/phpunit": "^4.8 || ^5.7",
"doctrine/annotations": "^1.2"
},
"suggest": {
"psr/log": "For optional PSR-3 debug logging",
"league/oauth2-google": "Needed for Google XOAUTH2 authentication",
"hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication",
"stevenmaguire/oauth2-microsoft": "Needed for Microsoft XOAUTH2 authentication",
"ext-mbstring": "Needed to send email in multibyte encoding charset",
"symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)"
},
"autoload": {
"psr-4": {
"PHPMailer\PHPMailer\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PHPMailer\Test\": "test/"
}
},
"license": "LGPL-2.1-only"
}
我没有使用过 EB,但根据您的假设,它会在需要时 运行 composer install
,您需要包含一个有效的 composer.json
.
您正在包含 软件包的 composer.json。因此,您安装的是 PHPMailer 的依赖项,而不是 PHPMailer 本身。
您需要为您的 project/application 创建自己的 composer.json
文件,在其中声明 PHPMailer 是您应用程序的依赖项。
例如:
{
"name": "your/app",
"type": "project",
"description": "",
"license": "CC-0",
"require": {
"phpmailer/phpmailer": "~6.1"
}
}
在任何情况下,您应该先尝试您的代码(包括原始依赖项安装)离线,然后再将其上传到 EB。
如果您尝试 运行 composer install
和 运行 离线您的项目,您会遇到同样的问题。在 部署运行ning 之前获取你的基本项目。
根据我对 Elastic Beanstalk 的理解,如果您在根目录中包含一个 composer.json
文件并且不包含供应商文件,那么 composer 将根据 JSON 指令自动处理该库。
composer.json
文件是根据 PHPMailer Github 上的当前第 51 行 json 文件,它位于我的 EB 根文件夹中 /public
和 .ebextensions
然后我有这个 PHP 脚本 (public/phphead.php
),从 AWS 文档中复制
// 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;
// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require '../vendor/autoload.php';
// requiring /vendor/autoload.php would throw a fatal error and so I presume this is where composer creates the vendor dir. no more fatal errors have been thrown and I have been looking to confirm this is the right location but I have only the lack of errors to make me think composer is found and working...
// ....
$mail = new PHPMailer(); //LINE 64 as per error log
EB 的错误日志输出:
PHP Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /var/app/current/public/phpHead.php:64\nStack trace:\n#0 /var/app/current/public/index.php(3): include()\n#1 {main}\n thrown in /var/app/current/public/phpHead.php on line 64
我正在使用的 composer.json
(与 PHPMailer 的 composer.json
文件相同,取自 GitHub):
{ "name": "phpmailer/phpmailer", "type": "library", "description": "PHPMailer is a full-featured email creation and transfer class for PHP", "authors": [ { "name": "Marcus Bointon", "email": "phpmailer@synchromedia.co.uk" }, { "name": "Jim Jagielski", "email": "jimjag@gmail.com" }, { "name": "Andy Prevost", "email": "codeworxtech@users.sourceforge.net" }, { "name": "Brent R. Matzelle" } ], "require": { "php": ">=5.5.0", "ext-ctype": "*", "ext-filter": "*" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.2", "phpunit/phpunit": "^4.8 || ^5.7", "doctrine/annotations": "^1.2" }, "suggest": { "psr/log": "For optional PSR-3 debug logging", "league/oauth2-google": "Needed for Google XOAUTH2 authentication", "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication", "stevenmaguire/oauth2-microsoft": "Needed for Microsoft XOAUTH2 authentication", "ext-mbstring": "Needed to send email in multibyte encoding charset", "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)" }, "autoload": { "psr-4": { "PHPMailer\PHPMailer\": "src/" } }, "autoload-dev": { "psr-4": { "PHPMailer\Test\": "test/" } }, "license": "LGPL-2.1-only" }
我没有使用过 EB,但根据您的假设,它会在需要时 运行 composer install
,您需要包含一个有效的 composer.json
.
您正在包含 软件包的 composer.json。因此,您安装的是 PHPMailer 的依赖项,而不是 PHPMailer 本身。
您需要为您的 project/application 创建自己的 composer.json
文件,在其中声明 PHPMailer 是您应用程序的依赖项。
例如:
{
"name": "your/app",
"type": "project",
"description": "",
"license": "CC-0",
"require": {
"phpmailer/phpmailer": "~6.1"
}
}
在任何情况下,您应该先尝试您的代码(包括原始依赖项安装)离线,然后再将其上传到 EB。
如果您尝试 运行 composer install
和 运行 离线您的项目,您会遇到同样的问题。在 部署运行ning 之前获取你的基本项目。