hafael / azure-mailer-driver

Provides Azure Communication Service integration for Symfony Mailer / Laravel.
MIT License
9 stars 5 forks source link

HttpClient is null in AzureMailerManager #10

Open oliverskawronek opened 9 months ago

oliverskawronek commented 9 months ago

Environment:

Error:

    "message": "Hafael\\Azure\\Transport\\AzureMailerApiTransport::__construct(): Argument #5 ($client) must be of type Symfony\\Contracts\\HttpClient\\HttpClientInterface, null given, called in ...\\vendor\\hafael\\azure-mailer-driver\\src\\AzureMailerManager.php on line 13",
    "exception": "TypeError",
    "file": "...\\vendor\\hafael\\azure-mailer-driver\\src\\AzureMailerApiTransport.php",

Please have look at AzureMailerManager

    protected function createAzureTransport()
    {
        $config = $this->app['config']->get('mail.mailers.azure', []);

        return new AzureMailerApiTransport(
            $config['endpoint'],
            $config['access_key'],
            $config['api_version'],
            boolval($config['disable_user_tracking']),
            $this->getHttpClient([]),
        );
    }

This method calls getHttpClient with an empty array.

But if you look at MailManager, with an empty array, the HttpClient will be always null:

/**
    protected function getHttpClient(array $config)
    {
        if ($options = ($config['client'] ?? false)) {
            $maxHostConnections = Arr::pull($options, 'max_host_connections', 6);
            $maxPendingPushes = Arr::pull($options, 'max_pending_pushes', 50);

            return HttpClient::create($options, $maxHostConnections, $maxPendingPushes);
        }
    }

When I replace it with HttpClient::create([], 3, 3) I'am able to send an email successfully.

But I don't know, what would be the right options for getHttpClient. So I will not create a pull request.

oliverskawronek commented 9 months ago

I did a little research on getHttpClient.

Guzzle as HTTP client was replaced by this pull request: https://github.com/laravel/framework/pull/45684 This is part of Laravel 9.x

I think the best way would be to allow a client option in the mail.php config:

'azure' => [
            'transport'             => 'azure',
            'resource_name'         => env('AZURE_MAIL_RESOURCE_NAME'),
            'endpoint'              => env('AZURE_MAIL_ENDPOINT', 'https://my-acs-resource-name.communication.azure.com'),
            'access_key'            => env('AZURE_MAIL_KEY'),
            'api_version'           => env('AZURE_MAIL_API_VERSION', '2023-03-31'),
            'disable_user_tracking' => env('AZURE_MAIL_DISABLE_TRACKING', false),
            'client' => [
                // ..
            ]
        ],

and pass $config to getHttpClient:

        return new AzureMailerApiTransport(
            $config['endpoint'],
            $config['access_key'],
            $config['api_version'],
            boolval($config['disable_user_tracking']),
            $this->getHttpClient($config),
        );

But if the client array is not set or empty, a null HttpClient would still returned. This behaviour from Laravel's MailManager#getHttpClientdoesn't make sense to me, but how about this?:

        // If no client options are set or empty, use default options.
        if (!isset($config['client']) || empty($config['client'])) {
            $config['client'] = HttpClientInterface::OPTIONS_DEFAULTS;
        }
oliverskawronek commented 9 months ago

I'am sorry, this is about v0.2.2 and now I realize that v0.3.0 is the current stable version :(

I just run composer require hafael/azure-mailer-driver and v0.2.2 was installed on my project due to v0.3.0 uses symfony/azure-mailer and this package has no stable version.

If you don't want to maintain v0.2.2 its ok to close this issue.

drmmr763 commented 3 months ago

I have this issue as well. I ran composer update and it bumped me from v0.2.1 to v0.2.2. Once upgraded I started getting the null mailer client issue.

I attempted to upgrade to v0.3.0 but unfortunately that requires PHP 8.2 which I am currently using v8.1 for other reasons. (Azure app services / devops pipelines are stuck on 8.1 currently).

The ultimate solution for me at this time is temporarily to downgrade the mailer package back to v0.2.1 for now. Just sharing in case anyone else has this problem.