EnglishAdam
EnglishAdam

Reputation: 1390

PHP Mailer class in Elastic Beanstalk not recognised

As I understand it with an Elastic Beanstalk if you include a composer.json file in root directory and DO NOT include a vendor file than composer will automatically handle the library from the JSON instructions.

The composer.json file is as per the current 51 line json file on PHPMailer Github and it sits in my EB root folder with /public and .ebextensions

Then I have this PHP script (public/phphead.php), copied from AWS docs

// 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

Error log outputs from the 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

The composer.json I'm using (same as PHPMailer's composer.json file, as taken from GitHub):

{
    "name": "phpmailer/phpmailer",
    "type": "library",
    "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
    "authors": [
        {
            "name": "Marcus Bointon",
            "email": "[email protected]"
        },
        {
            "name": "Jim Jagielski",
            "email": "[email protected]"
        },
        {
            "name": "Andy Prevost",
            "email": "[email protected]"
        },
        {
            "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"
}

Upvotes: 1

Views: 283

Answers (1)

yivi
yivi

Reputation: 47288

I haven't used EB, but based on your assumption that it will run composer install when needed, you need to include a valid composer.json.

You are including the package's composer.json. So you are installing the PHPMailer's dependencies, but not PHPMailer itself.

You need to create your own composer.json file for your project/application, where you declare PHPMailer a dependency for your app.

E.g.:

{
  "name": "your/app",
  "type": "project",
  "description": "",
  "license": "CC-0",
  "require": {
    "phpmailer/phpmailer": "~6.1"
    }
}

In any case, you should try your code (including original dependencies install) offline, before you upload it to EB.

If you tried to run composer install and running your project off-line, you would have hit the same problem. Get your basic project running before deployment.

Upvotes: 2

Related Questions