Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient is null in AzureMailerManager #10

Open
oliverskawronek opened this issue Feb 3, 2024 · 2 comments
Open

HttpClient is null in AzureMailerManager #10

oliverskawronek opened this issue Feb 3, 2024 · 2 comments

Comments

@oliverskawronek
Copy link

Environment:

  • PHP 8.2
  • hafael/azure-mailer-driver v0.2.2

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
Copy link
Author

I did a little research on getHttpClient.

Guzzle as HTTP client was replaced by this pull request: laravel/framework#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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant