FatalThrowableError in MailgunTransport.php line 67: Undefined class constant 'VERSION'

Viewed 93

I'm getting this error when trying to send email from within my Laravel 5.1 application. This function was working quite well for a very long time, but I stopped using my application for about a year and now when I try to use it I'm getting this error. I don't think I've made any changes to anything related to emails, so I'm not sure what the cause could be.

Here is what my composer.json looks like:

    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "barryvdh/laravel-ide-helper": "^2.1",
        "laravelcollective/html": "5.1.*",
        "nesbot/carbon": "1.39.*",
        "intervention/image": "^2.3",
        "fzaninotto/faker": "^1.7",
        "bugsnag/bugsnag-laravel": "^2.0",
        "silber/bouncer": " v1.0.0-rc.4",
        "doctrine/dbal": "v2.9",
        "spatie/laravel-medialibrary": "^4.0.0",
        "rap2hpoutre/laravel-log-viewer": "^2.2"
    },
    "require-dev": {
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },

EDIT:

Here is what the MailgunTransport.php file looks like:

<?php

namespace Illuminate\Mail\Transport;

use Swift_Mime_Message;
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\ClientInterface;

class MailgunTransport extends Transport
{
    /**
     * Guzzle client instance.
     *
     * @var \GuzzleHttp\ClientInterface
     */
    protected $client;

    /**
     * The Mailgun API key.
     *
     * @var string
     */
    protected $key;

    /**
     * The Mailgun domain.
     *
     * @var string
     */
    protected $domain;

    /**
     * THe Mailgun API end-point.
     *
     * @var string
     */
    protected $url;

    /**
     * Create a new Mailgun transport instance.
     *
     * @param  \GuzzleHttp\ClientInterface  $client
     * @param  string  $key
     * @param  string  $domain
     * @return void
     */
    public function __construct(ClientInterface $client, $key, $domain)
    {
        $this->key = $key;
        $this->client = $client;
        $this->setDomain($domain);
    }

    /**
     * {@inheritdoc}
     */
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $this->beforeSendPerformed($message);

        $options = ['auth' => ['api', $this->key]];

        $to = $this->getTo($message);

        $message->setBcc([]);

        if (version_compare(ClientInterface::VERSION, '6') === 1) {
            $options['multipart'] = [
                ['name' => 'to', 'contents' => $to],
                ['name' => 'message', 'contents' => $message->toString(), 'filename' => 'message.mime'],
            ];
        } else {
            $options['body'] = [
                'to' => $to,
                'message' => new PostFile('message', $message->toString()),
            ];
        }

        return $this->client->post($this->url, $options);
    }

    /**
     * Get the "to" payload field for the API request.
     *
     * @param  \Swift_Mime_Message  $message
     * @return array
     */
    protected function getTo(Swift_Mime_Message $message)
    {
        $formatted = [];

        $contacts = array_merge(
            (array) $message->getTo(), (array) $message->getCc(), (array) $message->getBcc()
        );

        foreach ($contacts as $address => $display) {
            $formatted[] = $display ? $display." <$address>" : $address;
        }

        return implode(',', $formatted);
    }

    /**
     * Get the API key being used by the transport.
     *
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * Set the API key being used by the transport.
     *
     * @param  string  $key
     * @return string
     */
    public function setKey($key)
    {
        return $this->key = $key;
    }

    /**
     * Get the domain being used by the transport.
     *
     * @return string
     */
    public function getDomain()
    {
        return $this->domain;
    }

    /**
     * Set the domain being used by the transport.
     *
     * @param  string  $domain
     * @return void
     */
    public function setDomain($domain)
    {
        $this->url = 'https://api.mailgun.net/v3/'.$domain.'/messages.mime';

        return $this->domain = $domain;
    }
}

Please note that this file is generated from the Mailgun source code, nothing in that file was created by me.

Line 67 is this one:

if (version_compare(ClientInterface::VERSION, '6') === 1)

After running composer update as suggested in the replies, I get the following warnings:

Package anahkiasen/underscore-php is abandoned, you should avoid using it. No replacement was suggested.
Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested.
Package jakub-onderka/php-console-color is abandoned, you should avoid using it. Use php-parallel-lint/php-console-color instead.
Package jakub-onderka/php-console-highlighter is abandoned, you should avoid using it. Use php-parallel-lint/php-console-highlighter instead.
Package jeremeamia/superclosure is abandoned, you should avoid using it. Use opis/closure instead.
Package mtdowling/cron-expression is abandoned, you should avoid using it. Use dragonmantank/cron-expression instead.
Package patchwork/utf8 is abandoned, you should avoid using it. Use symfony/polyfill-mbstring or symfony/string instead.
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Package symfony/debug is abandoned, you should avoid using it. Use symfony/error-handler instead.
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.

I'm wondering if maybe the swiftmailer is causing the issue...I'm not sure how to get it to use symfony mailer as recommended.

1 Answers

Because you said it was fine before, seems like you just need to update dependencies to their minor updates.

  1. Firstly, backup your composer.json file and run composer update and see it helps.
  2. If it doesn't help, you might need to install Guzzle. According to laravel doc,
    • you need to add "guzzlehttp/guzzle": "~5.3|~6.0" to your composer.json

    • delete composer.lock file

    • and run composer install

With composer.lock file presence, composer install will only look into that file and install versions defined in there.

enter image description here

  1. If it still not help, just replace the backed-up composer.json back and run composer install to rollback your dependencies. Then show me MailgunTransport.php file for further investigation.
Related